promise.html 1.29 KB
<html>
<head>
</head>
<body>
<div style="display:none">
<script>
'use strict';
let score = 0;
console.log("score 1",score);
var PromiseObj = new Promise(function(resolve,reject){
   score = 1;
   console.log("score 2",score);
   resolve('booooooooooooooooo')
});

PromiseObj.then((A)=>{
    console.log("A",A);
	console.log("score 3",score);
	score += 20;
	return 12
}).then((AA)=>{
    console.log("AA",AA,score);	
}).then(
    score = 0,	
).then((B)=>{
    console.log("B",B);
	console.log("score 5",score);
	return Promise.resolve().then(()=>{
	    console.log("score 6",score);
	    const C = score;
		console.log("c",C);
		if(score > 0){
		   throw new Error('zhong');
		}
	})
}).catch((D)=>{
   console.log("score 7",score);
console.log("d",D);
})
<!-- function runAsync(){ -->
    <!-- var p = new Promise(function(resolve, reject){ -->
        <!-- //做一些异步操作 -->
        <!-- setTimeout(function(){ -->
            <!-- console.log('执行完成'); -->
            <!-- resolve('随便什么数据'); -->
        <!-- }, 2000); -->
    <!-- }); -->
    <!-- return p;             -->
<!-- } -->
<!-- runAsync().then(function(data){ -->
    <!-- console.log(data); -->
    <!-- //后面可以用传过来的数据做些其他操作 -->
    <!-- //...... -->
<!-- }); -->
</script>
</div>
</body>
</html>