I'm learning promises with when.js
library, and using when.map
with node fs.readFile
makes me think I missed something.
foo
promise works ok when called as a single promise, but fails when used as a mapper function in when.map
because index is injected as a third parameter (and then callback is passed as 4th).
API doc says that when.map
has a requirement for mapper function to have two parameters. Then mapper function can be written as bar
, and it works in any context.
var when = require('when');
var node = require('when/node');
var _ = require('lodash');
// readFile has the same signature as fs.loadFile
function readFile(param1, param2, callback) {
console.log(Array.prototype.slice.call(arguments));
callback(null, [param1, param2]);
}
var foo = _.partialRight(node.lift(readFile), 'base64');
var bar = function (fileName, index) {
return node.lift(readFile)(fileName, 'base64');
};
when.map(['1', '2'], bar).done(); // works
when.map(['3', '4'], foo).done(); // does not work
Is there any more elegant way to write bar
function?