Can't Apply Lodash Partial To Function Created With Bluebird Promisifyall
The below code takes a all the methods in object lib and promisify's them. Then I can use the callback style function as a promise, which works. Then I use _.partial provide the fu
Solution 1:
Copying answer from the issue tracker:
The problem is _.partial
does not maintain the this
value which is required when you promisifyAll
. You can either use promisify
instead or you can use _.bind
which is the appropriate lodash method.
var o = {};
o.dummy = function(path, encoding, cb){
returncb(null, "file content here " + path + " " +encoding);
}
Promise.promisifyAll(o);
o.dummyAsync("a","b").then(function(data){
console.log("HI", data);
});
// and not `_.partial`var part = _.bind(o.dummyAsync, o, "a1", "b2");
part().then(function(data){
console.log("Hi2", data);
});
Solution 2:
promisifyAll
does create methods that expect to be called on the original instance (as it does invoke the original .dummy
method), but partial
does not bind the functions you pass in, so you are getting a this
error. You can use either readFile.call(lib)
or _.partial(lib.dummyAsync.bind(lib), …)
or just lib.dummyAsync.bind(lib, …)
.
Post a Comment for "Can't Apply Lodash Partial To Function Created With Bluebird Promisifyall"