new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("成功了")
reject("error message!")
},1000)
}).then(data=>{
console.log(data);
}).catch(error=>{
console.log(error);
})
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("成功了")
reject("error message!")
},1000)
}).then(data=>{
console.log(data);
},err=>{
console.log(err);
})
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("第1次成功了")
},1000)
}).then(data=>{
console.log(data)
return new Promise((res,rej)=>{
//发送第2次网络请求
setTimeOut(function(){
resolve("第2次成功了")
},1000)
})
}).then(data=>{
console.log(data)
return new Promise((res,rej)=>{
//发送第2次网络请求
setTimeOut(function(){
resolve("第3次成功了")
},1000)
})
}).then(data=>{
console.log(data)
});
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("第1次成功了")
},1000)
}).then(data=>{
console.log(data)
return new Promise((res,rej)=>{
resolve(data+"aaaa")
})
}).then(data=>{
console.log(data)
return new Promise((res,rej)=>{
resolve(data+"bbb")
})
}).then(data=>{
console.log(data)
});
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("第1次成功了")
},1000)
}).then(data=>{
console.log(data)
return Promise.resolve(data+"aaaa")
}).then(data=>{
console.log(data)
return Promise.resolve(data+"bbb")
}).then(data=>{
console.log(data)
});
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("第1次成功了")
},1000)
}).then(data=>{
console.log(data)
return data+"aaaa"
}).then(data=>{
console.log(data)
return data+"bbb"
}).then(data=>{
console.log(data)
});
new Promise((resolve,reject)=>{
//模拟网络请求
setTimeOut(function(){
resolve("第1次成功了")
},1000)
}).then(data=>{
console.log(data)
//return Promise.reject(data+"error")
throw data+"error"
}).then(data=>{
console.log(data)
return data+"bbb"
}).then(data=>{
console.log(data)
}).catch(err=>{
console.log(err)
});
Promise.all([
new Promise((resolve,reject)=>{
$ajax({url:"url1",sucess:function(data){
resolve(data)
}})
}),
new Promise((resolve,reject)=>{
$ajax({url:"url1",sucess:function(data){
resolve(data)
}})
}),
]).then(results=>{
//这里的results也是一个数组,results[0]是放的第1个Promise的结果 ,results[1]是放的第2个Promise的结果
console.log(results[0]);
console.log(results[1]);
});
原文:https://www.cnblogs.com/bqh10086/p/13183851.html