Bluebird promises a callback without an error argument

I am trying to promise a third-party library that does not use the callback(err, data) template callback(err, data) . Instead, they always return callback(data) and throw errors.

 Promise.promisifyAll(horse); var p = Promise.defer(); horse.drinkAsync() .error(function(data) { p.fulfill(data); }) .catch(function (err) { console.error('error occured', err); }); return p.promise; 

What is a good way to wrap this behavior with promises and still have it in order and resolve the captured error? The catch clause does not start and the application crashes.

+8
promise bluebird
source share
1 answer

From Bluebird 2.1 on, you can now configure promisifyAll using the special promisification handler:

 function noErrPromisifier(originalMethod){ return function promisified() { var args = [].slice.call(arguments); // might want to use smarter var self = this // promisification if performance critical return new Promise(function(resolve,reject){ args.push(resolve); originalMethod.apply(self,args); // call with arguments }); }; } var horse = Promise.promisifyAll(require("horse"), { promisifier: noErrPromisifier }); horse.drinkAsync().then(function(data){ // Can use here, ow promisified normally. }); 

If the original method is thrown asynchronously, there really is no way to wrap it in a domain, although I have never seen a library that works poorly.

+7
source share

All Articles