[英]How to make an ajax call for each element of an array of objects, using promises?
I have this code: 我有这个代码:
// _arrOfUsers = array of objects, each object represents a user.
//For example, a single object may be something like {name:"mike", id:"123"}
var _successUsers = function (_arrOfUsers) {
var promises = []; //empty array of promises
_arrOfUsers.forEach(function (user) {
promises.push(function () {
return $.Deferred(function (dfd) {
$.post("//sessionID",
queryServ.getAllUserDomainCommentsFunc("//currentDomainID", user.iD, "//currentDomainID", "//currentURL"),
'text', 'text/plain', this,
function (dataX) {
dfd.resolve(dataX);
});
}).promise();
});
});
$.when(promises).then(function (results) {
console.log(results);
});
};
Some elements to understand this code: 了解此代码的一些元素:
**** Ajax call settings **** Ajax通话设置
**** A comment is a jsonObject smilar to {id: "998", textOfcomment:"I commented this", target: "//targetID"}.
****注释是jsonObject smilar到
{id: "998", textOfcomment:"I commented this", target: "//targetID"}.
What I want to do is to make an ajax call for each user of the array "_arrOfUsers" to extract all his comments on the server; 我想要做的是为数组“_arrOfUsers”的每个用户进行ajax调用,以便在服务器上提取他的所有注释; after all the "extractions" I need to get all the results (that are all the comments from all users) and print them in the console.
在所有“提取”之后,我需要获得所有结果(这是所有用户的所有注释)并在控制台中打印它们。 The order is important for my code, this is why I'm using Promises.
顺序对我的代码很重要,这就是我使用Promises的原因。
I found the structure of this algorithm on a JSfiddle here on StackOverflow, but it doesn't work for me because it returns an array of functions and I can't understand why, probably because I've never used Promises during my career. 我在StackOverflow上的JSfiddle上找到了这个算法的结构,但它对我不起作用,因为它返回一个函数数组,我无法理解为什么,可能是因为我在职业生涯中从未使用过Promises。
Thank you in advance, if there's something not clear please feel free to tell me. 提前谢谢,如果有不清楚的地方请随时告诉我。
There's no need to create a deferred here since $.post()
already returns a promise and $.when()
keeps the results in order for you. 由于
$.post()
已经返回一个promise而$.when()
按顺序保存结果,因此无需在此处创建延迟。 Instead, you can do this: 相反,你可以这样做:
$.when.apply($, _arrOfUsers.map(function(user) {
return $.post("//sessionID", queryServ.getAllUserDomainCommentsFunc("//currentDomainID", user.iD, "//currentDomainID", "//currentURL"));
})).then(function() {
// all results available in order here as arguments to this callback
// arguments[0][0] is the first result
// arguments[1][0] is the second result
// arguments[2][0] is the third result and so on
for (var i = 0; i < arguments.length; i++) {
// arguments[i][0] is next one of your results
}
});
Note: I didn't quite understand what you were trying to do with the arguments to $.post()
(you seem to have made up some arguments the jQuery doc doesn't show) so I simplified it somewhat, but you may need to tweak those $.post()
arguments to suit your purpose. 注意:我不太明白你试图用
$.post()
的参数做什么(你好像已经$.post()
了jQuery doc没有显示的一些参数)所以我稍微简化了一下,但你可能需要调整那些$.post()
参数以满足您的目的。
Personally, I despise $.when()
and how it interacts with jQuery ajax promises (very confusing to use) so I would cast the ajax promise to an ES6 promise and use Promise.all()
(using Bluebird for promise support if older browser support is needed). 就个人而言,我鄙视
$.when()
以及它如何与jQuery ajax promises交互(非常混淆使用)所以我会将ajax承诺转换为ES6承诺并使用Promise.all()
(如果旧浏览器使用Bluebird进行承诺支持)需要支持)。
Promise.all(_arrOfUsers.map(function(user) {
return Promise.resolve($.post("//sessionID", queryServ.getAllUserDomainCommentsFunc("//currentDomainID", user.iD, "//currentDomainID", "//currentURL")));
})).then(function(results) {
// results is an array of results in order
});
Or if you include Bluebird, you can use: 或者,如果您包含Bluebird,您可以使用:
Promise.map(_arrOfUsers, function(user) {
return Promise.resolve($.post("//sessionID", queryServ.getAllUserDomainCommentsFunc("//currentDomainID", user.iD, "//currentDomainID", "//currentURL")));
}).then(function(results) {
// results is an array of results in order
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.