【问题标题】:Difference between async.each and async.eachSeriesasync.each 和 async.eachSeries 之间的区别
【发布时间】:2016-05-17 10:40:21
【问题描述】:

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.

【问题讨论】:

    标签: javascript node.js asynchronous


    【解决方案1】:

    您的代码示例与 async.each 所做的最相似,因为所有异步 request 调用都是同时进行的,并且允许并行进行。

    async.eachSeries的不同之处在于,每次迭代都会等待异步操作完成,然后再开始下一个。

    【讨论】:

      【解决方案2】:

      async.eachSeries() 将异步函数应用于系列数组中的每个项目。

      例如,假设您有一个用户列表,每个用户都需要将其个人资料数据发布到远程服务器日志。在这种情况下,顺序很重要,因为数组中的用户已排序。

      async.eachSeries(users, function(user, callback) {
        user.postProfileToServer(callback);
      });
      

      async.each() 将异步函数并行应用于数组中的每个项目。

      由于此函数将迭代器并行应用于每个项目,因此无法保证迭代器函数将按顺序完成。

      async.each(openFiles, saveFile, function(err){
      
      });
      

      【讨论】:

        【解决方案3】:

        可以用一个简单的例子来解释区别。

        假设我们有 3 个文件 1.txt, 2.txt, 3.txt。其中我们有文件 2.txt 的内容,大约 1GB 大小,所有其他文件都是具有最小文件大小的简单文件。

        linux/unix中使用如下命令可以生成1GB文件

        为文件2.txt创建一个1GB的文件 dd if=/dev/zero of=2.txt count=1024 bs=1024

        使用的目录结构

        .
        ├── files
        │   ├── 1.txt
        │   ├── 2.txt
        │   └── 3.txt
        ├── index.js
        ├── node_modules
        

        执行所需的 npm 安装并以不同方式尝试每个代码。

        对于 AsyncEachSeries

        let async = require('async');
        const fs  = require('fs');
        let files = ['./files/1.txt', './files/2.txt', './files/3.txt'];
        async.eachSeries(files, function(file, outCb)
        {
           fs.readFile(file, "utf8", (err, data) => {
            console.log(file);
            outCb();
          });
        },
        function(err)
        {
          console.log('all done!!!');
        });
        

        输出将是

        ./files/1.txt
        ./files/2.txt
        ./files/3.txt
        all done!!!
        

        对于 AsyncEach

        async.each(files, function(file, outCb)
        {
           fs.readFile(file, "utf8", (err, data) => {
              console.log(file);
              outCb();
          });
        },
        function(err)
        {
          console.log('all done!!!');
        });
        

        输出将是

        ./files/1.txt
        ./files/3.txt
        ./files/2.txt
        all done!!!
        

        结论:AsyncEachSeries等待每个操作完成 在进入下一个之前 AsyncEach 并行执行 2.txt 的文件太大,我们最后完成 只要。希望它能澄清差异。

        【讨论】:

          猜你喜欢
          • 2017-11-30
          • 1970-01-01
          • 2021-12-25
          • 2020-05-10
          • 2014-09-20
          • 2010-10-28
          • 2015-10-04
          • 2012-08-12
          • 2011-02-18
          相关资源
          最近更新 更多