我有一个功能:
function someFunction(params) {
//Creates an object
//$.each(collection, funct() { /*do work on collection, essentially pushes items into an array */});
//JSON.stringify(results)
//$.ajax POST...this is what I want $.when to wait for
}
我设置了
$.when(someFunction(params)
).then(doNextThing);
我也试过
$.when(function() {someFunction(params) }
).then(doNextThing);
在回调之前调用 doNextThing
。 $.each
或stringify
是否可能导致$.when
失败?
答案 0 :(得分:1)
someFunction
需要返回$.when
的对象才能正常工作。否则它相当于someFunction(params);$.when(undefined).then(doNextThing)
。在此设置中,永远不会调用doNextThing
。
由于您正在等待ajax调用,它应该看起来像这样:
function someFunction(params) {
...
return $.ajax(...);
}
$.when(someFunction(params)).then(doNextThing);