Bluebird promisify and callback with no error argument

别来无恙 提交于 2019-12-19 00:56:23

问题


I'm trying to promisify a 3rd party library that doesn't use the callback(err, data) pattern. Instead they always return callback(data) and throw on 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 nice way to wrap such a behavior with promises and still have it look ok and allow to catch the thrown error? The catch clause doesn't trigger and the application crashes.


回答1:


From Bluebird 2.1 on, you can now customize promisifyAll with a custom 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 throws asynchronously, there is really no way around wrapping it in a domain, although I've never seen a library that acts that poorly.



来源:https://stackoverflow.com/questions/24194808/bluebird-promisify-and-callback-with-no-error-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!