【发布时间】:2017-05-04 22:05:24
【问题描述】:
我有 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() 后,我正在解决延迟问题;
-
调用后端系统是通过什么
$.ajax?再说一遍:它是返回 Promise 还是 Deferred,还是...? -
它既不返回 Promise 也不返回 Deferred。这是使用其 JavaScript API 调用 Amazon Web Services - 例如,ec2.describeInstanceStatus(params, function(err, data) {... 这将返回服务器的当前状态
标签: javascript jquery promise deferred