javascript 带有嵌套 ajax 调用的 jQuery 延迟对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12273437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery deferred object with nested ajax calls
提问by Aesthete
I have a situation in which my ajax calls must perform in a particular order. I have used jQuery Deferred objects in other situations, but cannot seem to find a way to make this behave appropriately.
我有一种情况,我的 ajax 调用必须按特定顺序执行。我在其他情况下使用过 jQuery Deferred 对象,但似乎无法找到一种方法来使其行为适当。
I have a function which performs a number of ajax
requests in it's lifetime. Some of the requests will be performed during the success callback of other requests.
我有一个函数,它ajax
在它的生命周期中执行许多请求。某些请求会在其他请求成功回调期间执行。
My question: is there a way to return all nested deferred objects to the original $.when
call?
我的问题:有没有办法将所有嵌套的延迟对象返回到原始$.when
调用?
A simplified example would be:
一个简化的例子是:
function nestedAjax() {
$.get("/", function(){
console.log("First ajax done.");
$.get("/", function(){
console.log("Second ajax done.");
});
});
};
I am trying to have the nestedAjax
function to use $.when()
and $.done()
like so:
我正在尝试nestedAjax
使用该功能,$.when()
并且$.done()
喜欢这样:
$.when(nestedAjax()).done(function(){
console.log("Complete");
});?
With the console output reading:
随着控制台输出读数:
> First ajax done.
> Second ajax done.
> Complete.
I can return the first get
to achieve this:
我可以返回第一个get
来实现这一点:
> First ajax done.
> Complete.
> Second ajax done.
But obviously this is not what I require. Any help would be appreciated.
但显然这不是我所需要的。任何帮助,将不胜感激。
采纳答案by DrColossos
It's actually quite simple. Though all the AJAX calls are Deferred objects, I still use one for the method itself.
其实很简单。尽管所有 AJAX 调用都是 Deferred 对象,但我仍然为方法本身使用一个。
function nestedAjax() {
var dfd = $.Deferred();
$.get("/echo/json/", function(){
console.log("First ajax done.");
$.get("/echo/json/", function(){
console.log("Second ajax done.");
dfd.resolve();
});
});
return dfd.promise();
};
回答by dmnd
You don't actually need an extra deferred object. You can do what you want by chaining with then()
:
您实际上并不需要额外的延迟对象。你可以通过链接来做你想做的事then()
:
function nestedAjax() {
return $.get("/echo/json/").then(function(result1){
console.log("First ajax done.");
if (result1) {
return result1;
} else {
return $.get("/echo/json/").then(function(nestedResult){
console.log("Second ajax done.");
return nestedResult;
});
}
});
};
I added some logic since I think that's probably the reason you are executing this synchronously. After that you can use the result in $.when
like so:
我添加了一些逻辑,因为我认为这可能是您同步执行此操作的原因。之后,您可以$.when
像这样使用结果:
$.when(nestedAjax(), $.get("/something/else")).then(function(nested, other) {
console.log("Complete.", nested, other);
});
回答by Juw
Couldn′t add a comment for some reason to the above answer.
由于某种原因无法对上述答案添加评论。
So i add my comment here. The above answer will only work if the ajax calls are fast and returns BEFORE the return dfd.promise().
所以我在这里添加我的评论。上面的答案只有在 ajax 调用很快并且在返回 dfd.promise() 之前返回时才有效。
I have the same problem. And as you can see. The returned deferred object states that it is "pending": http://jsfiddle.net/BtEKa/
我也有同样的问题。正如你所看到的。返回的延迟对象表明它是“待处理的”:http: //jsfiddle.net/BtEKa/