简体   繁体   English

用蓝鸟承诺已经基于诺言的图书馆

[英]Promisifying already promise based library with bluebird

If a library exposes a promise-based API that is not based on bluebird but does not expose a traditional callback API, is there a way to "promisify" that library to return bluebird promises? 如果一个库公开了一个不基于bluebird的基于promise的API,但没有公开一个传统的回调API,是否有办法“承诺”该库以返回bluebird的promise?

Currently I either just return the promise to a bluebird context, or if I want to use any bluebird specific functions directly then I wrap the call with bluebird's Promise.resolve. 目前,我只是将promise返回到bluebird上下文中,或者如果我想直接使用任何特定于bluebird的函数,那么我将调用与bluebird的Promise.resolve打包在一起。

I believe this would be possible with ES2015 proxies, but neither Node.js™, io.js nor Babel support them. 我相信使用ES2015代理可以做到这一点,但是Node.js™,io.js和Babel都不支持它们。

Is there a sane way to do this without proxies? 有没有代理的明智方法吗?

Use the promisifier option 使用Promisifier选项

Promise.promisifyAll(lib, {
     promisifier: function(fn) {
         return function () {
              return Promise.resolve(fn.apply(this, arguments));
         }
     }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM