//封装一个promise
var ajaxPromise = function(param){
return new Promise(function(suc,fail){
debugger;
console.log(param)
//ajax请求
$.ajax({
url:param.url,
type:"post",
async:false,
data:param.data,
dataType:"json",
success: function(res) {
suc(res);
},
error: function(err) {
fail(err);
}
})
})
}
var step1 = ajaxPromise({url:‘Student_getStudentInfo‘,data:{phoneNum:sessionPhoneNum}})
.then(function(res){
console.log("jjjjj");
//返回一个新的之前封装的promise
return ajaxPromise({url:"ApplyForm_getSchoolList",data:{}})
})
.then(function(res){
console.log("gsjgj");
},function(err){
console.log("jfdhskjfhsdfhsj");
})
.catch(function(err){
console.log(ajaxPromise);
console.log("fkdjskjsdk");
})
});
then方法里面可以有两个函数 第一个是当将Promise的状态变为fuifill的时候,后面一个函数是将promise状态变为reject的时候,
但是其实catch也可以处理这样的reject 以及本来就需要处理的异常的报错所以不能简单党的将then理解为请求成功时执行的
原文:https://www.cnblogs.com/theworldofbeisong/p/9533120.html