问题描述
下面是与承诺我多AJAX调用。
$(窗口).load(函数(){
$。当(getApiModemList())。完成(功能(供应商){
VAR deferreds = calculateApiBalances(供应商);
$ .when.apply($,deferreds).done(函数(平衡){
执行console.log(平衡);
的console.log(全部完成);
});
});
功能getApiModemList(){
返回$ .getJSON(URL);
}
功能calculateApiBalances(供应商)
{
变种延迟= [];
$每个(供应商,功能(K,V){
defer.push($的getJSON(someurl));
});
返回延迟;
}
});
功能calculateApiBalances()返回我一些平衡,我需要总结得到总的所有余额。但是,当打印的console.log(平衡)是亘古不变的我提供均衡JSON的有效阵列。另一个问题是,如果我的ajax调用的calculateApiBalances()的任何一个失败,它亘古不变的版画全部完成。我应该在上面code,可以达到这一点。
这是一个奇怪的事情 $的。当
。它不为你提供一个数组,但调用你的回调多个参数。
是的,因为当一个人的承诺未能全 $。当()
承诺立即拒绝,你没有任何错误处理程序。你必须单独捕获错误,如果你想总是得到(可能无效响应)的阵列。另请参见 $ .Deferred:如何在每一个承诺一直执行检测
首先,避免延迟反模式: - )
$(窗口).load(函数(){
getApiModemList(),然后(calculateApiBalances)。然后(函数(){
VAR余额= Array.prototype.slice.call(参数);
执行console.log(平衡);
的console.log(全部完成);
});
});
功能getApiModemList(){
返回$ .getJSON(somurl)。然后(函数(RES){
返回res.data;
});
}
功能calculateApiBalances(供应商){
返回$ .when.apply($,$ .MAP(供应商,功能(V,K){
返回$ .getJSON(someurl)。然后(空,$。当);
}));
}
修改由漫游者:
这是一个版本的主程序的一个机制,总结了平衡。
getApiModemList(),然后(calculateApiBalances)。然后(函数(){
VAR sumOfBalances = Array.prototype.reduce.call(参数,功能(TOT,OBJ){
返回托特+(+ obj.balance || 0);
},0);
执行console.log(sumOfBalances);
的console.log(全部完成);
});
OBJ
是 $许诺的对象。的getJSON(someurl)
在 calculateApiBalances( )
。在 $。的getJSON()
错误的情况下, OBJ
将是一个jqXHR对象, obj.balance
将未定义
和 + obj.balance
将 NaN的
,因此默认为加零;否则添加 obj.balance
。
如果你想知道有多少的getJSON请求是成功的,有多少是不成功的,然后还有一些code写的,但不是很多。
Below are my Multiple ajax calls with promises.
$(window).load(function(){
$.when(getApiModemList()).done(function(vendors){
var deferreds = calculateApiBalances(vendors);
$.when.apply($,deferreds).done(function(balance) {
console.log(balance);
console.log("All Done");
});
});
function getApiModemList(){
return $.getJSON("url");
}
function calculateApiBalances(vendors)
{
var defer=[];
$.each(vendors,function(k,v){
defer.push($.getJSON(someurl));
});
return defer;
}
});
Function calculateApiBalances() return me some balance which I need to Sum up to get total of all balance .But when print console.log(balance) it doesnot provide me with valid array of balance json.Another issue is if any one of my ajax calls in calculateApiBalances() fails it doesnot prints All Done.What should be done in above code to achieve this.
That's a weird thing of $.when
. It doesn't provide you with an array, but calls your callback with multiple arguments.
Yes, because when one promise fails the whole $.when()
promise is rejected immediately, and you don't have any error handler. You'll have to catch errors individually if you want to always get an array (of possibly invalid responses). See also $.Deferred: How to detect when every promise has been executed.
First of all, avoid the deferred antipattern :-)
$(window).load(function() {
getApiModemList().then(calculateApiBalances).then(function() {
var balance = Array.prototype.slice.call(arguments);
console.log(balance);
console.log("All Done");
});
});
function getApiModemList() {
return $.getJSON(somurl).then(function(res) {
return res.data;
});
}
function calculateApiBalances(vendors) {
return $.when.apply($, $.map(vendors, function(v, k) {
return $.getJSON(someurl).then(null, $.when);
}));
}
EDIT by Roamer:
And here's a version of the master routine with a mechanism for summing the balances.
getApiModemList().then(calculateApiBalances).then(function() {
var sumOfBalances = Array.prototype.reduce.call(arguments, function(tot, obj) {
return tot + (+obj.balance || 0);
}, 0);
console.log(sumOfBalances);
console.log("All Done");
});
obj
is the object promised by $.getJSON(someurl)
in calculateApiBalances()
. In the case of a $.getJSON()
error, obj
will be a jqXHR object, obj.balance
will be undefined
and +obj.balance
will be NaN
, therefore default to adding zero; otherwise add obj.balance
.
If you wanted to know how many of the getJSON requests were successful and how many were unsuccessful, then there's some more code to write, but not a lot.
这篇关于获取jQuery的多个延迟对象的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!