.then 方法
连续两天看到这个.then 方法 肯定不是巧合 ,容我百度下:.then 字面意思就是上一步执行完了,执行下一步,不过这是 Promise 对象的方法,非 Promise 对象没有 then 方法。在 jQuery 中 Promise 叫作 Deferred 对象。
function timeout(duration){
return new Promise(function(resolve,reject){
setTimeout(resolve,duration);
});
}
var m = timeout( 5000).then(()=>{
console.log('hahah')
})
1.then() 方法是异步执行;
2.意思是:就是当.then() 前的方法执行完后再执行then() 内部的程序,这样就避免了,数据没有获取到等的问题;
3.语法 promise.then(onCompleted,onRejected);
4.参数 :
4.1 promise必需 。 promise对象;
4.2 onCompleted必需。承诺成功完成是要运行的履行处理程序函数。
4.3 onRejectrd 可选。承诺被拒绝是要运行的错误处理程序函数。
页:
[1]