function Async1(){
var p =new Promise(function(resolve,reject){
setTimeout(function(){
console.log('异步任务1执行完成');
resolve('1中的成功数据')
},1000)
});
return p
}
function Async2(){
var p =new Promise(function(resolve,reject){
setTimeout(function(){
console.log('异步任务2执行成功');
resolve('2中的成功数据')
},2000)
})
return p;
}
function Async3(){
var p = new Promise(function(resolve,reject){
setTimeout(function(){
console.log('异步任务3执行成功');
resolve('3中的成功数据')
},3000)
});
return p;
}
Async1()
.then(function(data){
console.log('Async1中的data',data)
return Async2()
})
.then(function(data){
console.log('Async2中的data',data)
return Async3()
})
.then(function(data){
console.log('最后拿到Async3中的数据')
})
function getNumber(){
var p = new Promise(function(resolve,reject){
setTimeout(function(){
var num = Math.ceil(Math.random()*10);
if(num<=5){
resolve(num)
}else{
reject('数字大于5了')
}
},2000)
});
return p
}
getNumber()
.then(
function(data){
console.log('resolved');
console.log(data)
},
function(reason,data){
console.log('rejected');
console.log(reason)
}
)
console