结合延期响应 [英] Combining deferred responses
本文介绍了结合延期响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery Deferred的 $。当
将响应组合成数组时是否可能?
Is it possible, when using jQuery Deferred's $.when
to combine the responses into an array?
var promise = $.when(
ajaxRequest1,
ajaxRequest2
);
promise.done(callback);
回调函数看起来像函数回调(resp,options)
。请注意它只接受一个回复。
The "callback" function looks like function callback(resp, options)
. Note how it only accepts a single response.
我认为以下可能有用,但没有。
I thought the following might work, but did not.
var promise = $.when(
ajaxRequest1,
ajaxRequest2
);
promise.then(function(resp1, resp2) {
return [resp1, resp2];
});
推荐答案
问题是jQuery解决了它的ajax承诺3参数,当您在多个承诺上使用 $。时
时,它将成为一个值数组。要仅获取数据
(每个ajax回调的第一个参数),请使用
The problem is that jQuery resolves its ajax promises with 3 arguments, which then become an array of values when you are using $.when
on multiple promises. To get only the data
(the first argument of each ajax callback), use
var promise = $.when(
ajaxRequest1,
ajaxRequest2
).then(function(resp1, resp2) {
return [resp1[0], resp2[0]];
});
这篇关于结合延期响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文