Skip to content Skip to sidebar Skip to footer

How To Promisify?

I have this sort of function: someFunction.someMethod('param1', function(err, res1, res2) { req.method(res1, function(err) { if (!err) { console.log('Yes!'); } })

Solution 1:

I would try this:

var a = Promise.promisify(function(arg, cb) {
    someFunction.someMethod(arg, cb)(req, res);
});

a('param1').spread(function(res1, res2) {
  console.log('Yes!');
}).catch(function(err) {

});

…and yes, it's ugly. This partial application might however be a sign that the function returned by someFunction.someMethod('param1', …) is supposed to be called multiple times; and your callback would be called as many times - where you cannot use promises any more.

Post a Comment for "How To Promisify?"