Javascript - 完成所有嵌套forEach循环后的回调

时间:2017-03-27 16:43:24

标签: javascript node.js async.js

我确定这是一项相当简单的任务,但此时我无法绕过它。我有一组嵌套的forEach循环,我需要在所有循环完成运行时进行回调。

我打开使用async.js

我正在与之合作:

const scanFiles = function(accounts, cb) {
  let dirs = ['pending', 'done', 'failed'];
  let jobs = [];

  accounts.forEach(function(account) {
    dirs.forEach(function(dir) {
      fs.readdir(account + '/' + dir, function(err, files) {
         files.forEach(function(file) {
            //do something
            //add file to jobs array
            jobs.push(file);
         });
      });
    });
  });

  //return jobs array once all files have been added
  cb(jobs);
}

6 个答案:

答案 0 :(得分:5)

使用forEach的第二个参数(索引),您可以检查每次运行最内层循环时是否完成所有循环。

因此,只需在代码中添加几行即可:

const scanFiles = function(accounts, cb) {
    let dirs = ['pending', 'done', 'failed'];
    let jobs = [];

    accounts.forEach(function(account, accIndex) {
        dirs.forEach(function(dir, dirIndex) {
            fs.readdir(account + '/' + dir, function(err, files) {
                files.forEach(function(file, fileIndex) {
                    //do something
                    //add file to jobs array
                    jobs.push(file);

                    // Check whether each loop is on its last iteration
                    const filesDone = fileIndex >= files.length - 1;
                    const dirsDone = dirIndex >= dirs.length - 1;
                    const accsDone = accIndex >= accounts.length - 1;

                    // all three need to be true before we can run the callback
                    if (filesDone && dirsDone && accsDone) {
                        cb(jobs);
                    }
                });
            });
        });
    });
}

答案 1 :(得分:2)

更简单的解决方案

无需循环并推送到数组

我注意到这里的所有答案都使用了很多复杂的代码。 你可以更简单:

let fs = require('mz/fs');
let path = require('path');

let d = ['pending', 'done', 'failed'];
let a = ['A', 'B', 'C']; // <-- example accounts

let paths = [].concat.apply([], d.map(d => (a.map(a => path.join(d,a)))));
Promise.all(paths.map(path => fs.readFile(path, 'utf-8'))).then(files => {
  // you have all data here
}).catch(error => {
  // handle errors here
});

说明

如果您使用fs的承诺版本 - 目前您可以使用:

let fs = require('mz/fs');

使用mz模块:

很快它将成为Node中的原生,请参阅:

然后你就可以做下面的代码了。使用数据:

// directories:
let d = ['pending', 'done', 'failed'];
// accounts:
let a = ['A', 'B', 'C'];

您可以轻松创建路径数组:

let paths = [].concat.apply([], d.map(d => (a.map(a => path.join(d,a)))));

您可以从中创建承诺数组:

let promises = paths.map(path => fs.readFile(path, 'utf-8'));

您甚至可以使用Promise.all()读取所有文件:

let data = Promise.all(promises);

现在你可以使用以下所有内容:

data.then(files => {
  // you have everything ready here
}).catch(error => {
  // some error happened
});

注意:您需要两个模块才能使上述代码正常工作:

let fs = require('mz/fs');
let path = require('path');

答案 2 :(得分:0)

您可以使用walk

  walker.on("end", function () {
    console.log("all done");
    cb(jobs);
  });

答案 3 :(得分:0)

简单的计数器

一个简单的方法就是保持一个柜台。

const scanFiles = function(accounts, cb) {
  let dirs = ['pending', 'done', 'failed'];
  let jobs = [];

  // Variables to keep track of
  const lastAccountIndex = accounts.length * dirs.length;
  let indexCounter = 0;

  accounts.forEach(function(account) {
    dirs.forEach(function(dir) {  
      fs.readdir(account + '/' + dir, function(err, files) {
        files.forEach(function(file) {
          //do something
          //add file to jobs array
          jobs.push(file);

          indexCounter++;
        });

        //return jobs array once all files have been added
        if (lastAccountIndex === indexCounter) {
          cb(jobs);
        }
      });
    });
  }); 
}

无极

或者,fs + promise在这里非常有用。

const scanFiles = function(accounts) {
  let dirs = ['pending', 'done', 'failed'];
  let jobs = [];

  const filePromises = []; 
  accounts.forEach(function(account) {
    dirs.forEach(function(dir) {
      filePromises.push(new Promise((resolve, reject) => {
        fs.readdir(account + '/' + dir, function(err, files) {
          files.forEach(function(file) {
            resolve(file);
          });
        });
      }));
    });
  });
  return Promise.all(filePromises);
}

scanFiles(someAccounts)
.then((files) => {
    files.forEach((file) => {
    // At this point, iwll the files will be scanned
    // So, do whatever you want with all the files here.
  });
});

FS-诺

或者只使用https://www.npmjs.com/package/fs-promise

答案 4 :(得分:0)

如果您使用asyc库https://caolan.github.io/async/docs.html,您的代码将会快得多。 (forEach正在阻止[JavaScript, Node.js: is Array.forEach asynchronous?)。

const scanFiles = function (accounts, cb) {
let dirs = ['pending', 'done', 'failed'];
let jobs = [];

async.each(accounts, function (account, accountCallback) {
    async.each(dirs, function (dir, dirCallback) {

        fs.readdir(account + '/' + dir, function (err, files) {
            if(err) console.log(err);

            async.each(files, function (file, fileCallback) {
                //do something
                //add file to jobs array
                jobs.push(file);
                fileCallback();

            }, dirCallback);

        });
    }, accountCallback);
}, function (err) {
    //return jobs array once all files have been added
    if (err) throw err;
    cb(jobs)
});

};

答案 5 :(得分:-1)

所以问题是你在fs.readdir完成之前发送了一个空结果,因为nodeJS是异步的。所以解决方案是在fs.readdir函数中添加回调。

const scanFiles = function (accounts, cb) {
    let dirs = ['pending', 'done', 'failed'];
    let jobs = [];

    accounts.forEach(function (account, i) {
        dirs.forEach(function (dir, j) {
            fs.readdir(account + '/' + dir, function (err, files) {
                files.forEach(function (file, k) {
                    //do something
                    //add file to jobs array
                    jobs.push(file);
                });
                if (i === accounts.length - 1 && j === dirs.length - 1 && k === files.length - 1) {
                    //return jobs array once all files have been added
                    cb(jobs);
                }
            });
        });
    });
}