Promise异步控制代码实例
一、模拟3次ajax异步请求
function a(){
return new Promise(function(resolve,reject){
$.post('http://mess.ityzm.com/a.php',function(r){
resolve(r);
});
});
}
function b(){
return new Promise(function(resolve,reject){
$.post('http://mess.ityzm.com/b.php',function(r){
resolve(r);
});
});
}
function c(){
return new Promise(function(resolve,reject){
$.post('http://mess.ityzm.com/c.php',function(r){
resolve(r);
});
});
}
二、模拟实际开中的ajax异步嵌套流程
a().then(function(r){
b().then(function(r){
c().then(function(r){
console.log(r);
});
});
});
或
a().then(function(r){
return b();
}).then(function(r){
return c();
}).then(function(r){
console.log(r);
});
参考博客:https://www.cnblogs.com/lvdabao/p/es6-promise-1.html
1 COMMENT