蓝鸟promisify和callback没有错误的参数

我试图promisify不使用callback(err, data)模式的第三方库。 相反,他们总是返回callback(data)throw错误。

 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; 

用promise来包装这样一个行为的好方法是什么,仍然有它看起来不错,并允许捕捉抛出的错误? catch子句不触发,应用程序崩溃。

从Bluebird 2.1开始,您现在可以使用自定义的promisification处理程序来自定义promisifyAll:

 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. }); 

如果原始方法asynchronous抛出,那么真的没有办法绕过它在一个域中,虽然我从来没有看到一个行为不好的图书馆。