【发布时间】:2017-06-09 22:03:22
【问题描述】:
我有一个 test_func 需要按顺序执行,即; first_call、second_call、third_call。
当前输出:-
started == first_call
VM81:49 status in : first_call pending
VM81:47 started == second_Call
VM81:47 started == third_Call
VM81:49 status in : second_Call pending
VM81:49 status in : third_Call pending
function test_func(call_from){
var deferred = $.Deferred();
console.log('started ==',call_from)
setTimeout(function(){
console.log("status in :",call_from + ' ' + deferred.state());
deferred.resolve();
},5000);
return deferred.promise();
};
test_func('first_call').then(function(){
test_func('second_Call')
}).then(function(){
test_func('third_Call')
})
【问题讨论】:
-
第一个
.then回调需要return test_func('second_Call') -
我也可以建议异步吗?它是一个提供大量函数的库,可帮助您应对异步编程带来的控制流和其他挑战。
-
将 Promise 转换为回调 @newBee ?
标签: javascript jquery