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')
.