我有3个函数对后端系统进行异步调用:fnOne,fnTwo,fnThree。我知道我做得不对,但无法找出正确的方法。延期承诺 - 每次完成后逐一运行函数
我出发,试图打电话给他们一个由各完成后:
然而,fnOne已经有一个延迟对象在里头时解决它是解决我的诺言。
$.when(oController._fnOne()).done(function(){
console.log("fn one complete");
});
功能
_fnOne: function(){
var oController = this;
//when the deferred is done in _checkInstanceStatus, it is resolving the above, rather than the pDeferred in this function resolving the above.
$.when(oController._checkInstanceStatus(oParams)).done(function(oStatusData) {
//does some stuff
return $.Deferred(function() {
var pDeferred = this;
//does a call for data and upon success i will resolve ..
pDeferred.resolve();
});
});
}
其他功能
_checkInstanceStatus: function(oParams){
return $.Deferred(function() {
var pDeffered = this;
//does a call for data and upon success i will resolve...
pDeffered.resolve(data);
});
});
},
然后该计划是尝试把它们连,所以他们运行一个接一个,像这样:
$.when(oController._fnOne())
.then(oController._fnTwo())
.then(oController._fnThree())
.done(function(){
console.log("all complete!");
});
恐怕是不太足够的信息,在这个问题给你一个坚实的答案。例如,“fnOne”所做的“打电话”是什么?这是否会返回Promise或Deferred? –
@ T.J.Crowder感谢您的回复,调用是调用数据后端系统的地方,在解决延迟后,调用已成功完成pDeferred.resolve(); – neeko
这个对后端系统的调用是通过'$ .ajax'来实现的吗?再次说明:它是否会返回Promise或Deferred,或者? –