async.apply inside async.waterfall

孤人 提交于 2019-12-08 08:10:30

问题


I have the following snippet of code

async.waterfall([
  // Read directory
  async.apply(fs.readdir, '../testdata'),
  // Load data from each file
  function(files, callback) {
    async.each(files, loadDataFromFile, callback);
  }
], function(err) {
  if (err) {
    api.logger.error('Error while inserting test data', err);
  }
  next();
});

Is there a way I could replace this piece:

function(files, callback) {
  async.each(files, loadDataFromFile, callback);
}

with just a function? Like I did above, using async.apply() I replaced this:

function(callback) {
  fs.readdir('../testdata', callback);
}

I know I could create my own helper function to do this, or I can leave it like this, but I was wondering if there's a way to do this using only functions like .bind() or .apply().

I've thought about using .bind() then .apply() but this would result in function(loadDataFromFile, files, callback) which is not okay.


回答1:


I was wondering if there's a way to do this using only functions like .bind() or .apply().

Not using only native functions, or only those from async it seems. As you have noticed, one would need to flip the each function. Some implementations of partial application (like Underscore) allow intermediate arguments, but you would need to explicitly include them.

An example with lodash's partialRight:

async.waterfall([
  _.partial(fs.readdir, '../testdata'), // Read directory
  _.partialRight(async.each, loadDataFromFile), // Load data from each file
], function(err) {
  if (err)
    api.logger.error('Error while inserting test data', err);
  next();
});

Possible you will need to bind the method to the correct context though, like fs.readdir.bind(fs, '../testdata').



来源:https://stackoverflow.com/questions/22714355/async-apply-inside-async-waterfall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!