问题描述
我想实现如下因素的情况下,使用JQuery推迟,没有多少运气。
I'm trying to implement the folowing scenario, using JQuery deferred, without much luck.
你会用哪些部分递延API,以及你将如何构建你的电话来实现以下目标:
What parts of the deferred api would you use, and how would you structure your calls to achieve the following:
1日AJAX的马蹄莲以serviceA找回ID列表
1st ajax callA to serviceA retrieve a list of Ids
等待,直到此调用返回
则n AJAX调用serviceB,每个呼叫使用使用ID由马蹄莲返回列表
then n ajax calls to serviceB, each call using a using an Id from the list returned by callA
等待,直到所有serviceB调用已经返回
wait until all serviceB calls have returned
那么最终的Ajax调用serviceC
then a final ajax call to serviceC
推荐答案
您可以做这样的(或多或少伪code):
You could do like this (more or less pseudocode):
(function() {
// new scope
var data = []; // the ids coming back from serviceA
var deferredA = callToServiceA(data); // has to add the ids to data
deferredA.done(function() { // if callToServiceA successful...
var deferredBs = [];
for i in data {
deferredBs.push(callToServiceB(...));
}
$.when.apply($, deferredBs).then(callToServiceC);
});
}());
在 callToServiceX
函数返回由 $返回的承诺对象。AJAX
。
有可能是一个干净的解决方案不是让数据
在一个共享的范围,用的,但是设置会更有点困难(不一定更具可读性)。
There might be a "cleaner" solution than having data
in a shared scope, with resolve
, but the setup would be a bit more difficult (and not necessarily more readable).
这篇关于jQuery的延迟 - 我需要管道或链来实现这种模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!