【发布时间】:2015-10-17 00:45:57
【问题描述】:
我有一个数组,我需要通过http post请求将数组的值一一发送到webservice。对于 node.js ,我正在使用“async”包来做到这一点,例如:async.eachSeries 做得很好,我怎样才能为 angular.js 做同样的事情,我的正常异步代码;
//this code sends all queries of array (maybe 5.000 request at same time , it is hard to process for webservice :=) ) at same time and wait for all responses.
//it works but actually for me , responses should wait others at end of loop should work one by one
//like async.eachSeries module!
for (var i = 0; i < myArr.lenght; i++) {
(function (i) {
var data = {
"myQuery": myArr[i].query
};
$http.post("/myServiceUrl", data).success(function (result) {
console.log(result);
});
})(i);
}
Matt Way 和 Chris L 都回答 Correct ,您可以研究 Chris 的回答以了解 for 循环中的异步同步功能。
【问题讨论】:
-
顺序重要吗?
-
你可以在浏览器中使用
async包和angular的$http。您唯一遇到的问题是,如果您想用结果更新您的$scope,那么您可能需要使用$scope.$apply或$timeout(没有可选延迟)
标签: javascript angularjs node.js asynchronous