Osm*_*rdi 9 javascript asynchronous node.js
async.each是作为异步数组迭代工作的吗?
async.eachSeries是否作为同步数组迭代工作?(它实际上等待响应)
我问这些是因为它们都有回调但async.each就像异步数组迭代一样对ex:
//This is traditional way to iterate an array with callback functions in node.js
//Is this same with async.each ? i want to know it actually.
for (var i = 0; i < data.length; i++) {
(function (i) {
request(data[i],function(body){
console.log(body)
});
})(i);
//if this codes and async.each are doing same things ,
//i know that async gives me an aert when all finished thats the difference.
Run Code Online (Sandbox Code Playgroud)
Joh*_*yHK 13
您的代码示例与所做的代码示例最相似async.each
,因为所有异步request
调用都是立即进行的,并且允许并行执行.
不同之async.eachSeries
处在于每次迭代都会等待异步操作完成,然后再开始下一次操作.
async.eachSeries()将异步函数应用于系列数组中的每个项目。
例如,假设您有一个用户列表,每个用户都需要将其配置文件数据发布到远程服务器日志中。在这种情况下,顺序很重要,因为阵列中的用户已排序。
async.eachSeries(users, function(user, callback) {
user.postProfileToServer(callback);
});
Run Code Online (Sandbox Code Playgroud)
async.each()将异步函数并行应用于数组中的每个项目。
由于此函数将迭代器并行应用到每个项目,因此不能保证迭代器功能将按顺序完成。
async.each(openFiles, saveFile, function(err){
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13378 次 |
最近记录: |