使用bluebird创建支持节点回调和承诺的节点库 [英] Creating node library that support both node callbacks and promise with bluebird

查看:90
本文介绍了使用bluebird创建支持节点回调和承诺的节点库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个节点模块,我希望能够支持节点回调和Promise API。我听到最好的东西(主要是最快的)的图书馆是 bluebird 。因此,在阅读了一些文档并查看其他一些使用bluebird的库之后,我认为这将是获得支持节点回调和Promise API的方法的最简洁方法:

I am creating a node module and I want to be able to support both node callback and Promise APIs. The library that I hear the best things about (mainly that is it the fastest) is bluebird. So after reading some docs and looking at some other libraries that are using bluebird, I thought this would be the cleanest way to get a method to support both node callback and Promise APIs:

this.isAllowed = function(role, resource, permission, callback) {
  var isAllowedAsync = bluebird.promisify(isAllowed);
  return isAllowedAsync(role, resource, permission).nodeify(callback);
};

但是使用此代码,回调永远不会执行。经过一些研究,我尝试了这个:

However with this code, the callback is never executed. After some more research, I tried this:

this.isAllowed = function(role, resource, permission, callback) {
  return new bluebird(function (resolve, reject) {
    resolve(isAllowed(role, resource, permission));
  }).nodeify(callback);
};

使用该代码,节点回调和Promise API都能正常工作。

With that code, both the node callback and Promise API works.

作为参考,这是isAllowed方法:

For reference, this is the isAllowed method:

var isAllowed = function(role, resource, permission) {
  if(!lists[role] || !lists[role][resource]) {
    return false;
  }

  return lists[role][resource].indexOf(permission) !== -1;
};

我在第一个代码示例中做错了什么,或者第二个例子是获得什么的真正方法我在寻找?

Am I doing something wrong in the first code example or is the second example the real way of getting what I am looking for?

推荐答案

你的具体问题毫无意义(请参阅我的评论),所以我只是通用的。有两种方法可以公开双承诺/回调API。

Your specific problem makes no sense (see my comment) so I'll just be generic. There are 2 ways to expose a dual promise/callback API.

第一种方法是同时使用相同的函数支持promises和callbacks,如果没有传递callback参数则返回一个promise,如果传递则使用回调。

First way is to have the same function support promises and callbacks at the same time, by returning a promise if callback parameter is not passed or using the callback if it is passed.

然而,当一些参数是可选的时,这很难实现并且可能有一些令人讨厌的问题。

However this is hard to implement and can have some nasty problems when some arguments are optional.

To实现它,除了在返回的链的末尾添加 .nodeify 之外,你做的事与100%承诺函数完全相同。

To implement it, you do exactly the same thing as you would with a 100% promise function except you add a .nodeify at the end of the returned chain.

// Note however this doesn't work if some arguments before 
// `callback` are optional
function dualApi(..., callback) {
    // acting as if you would never support callbacks at all
    return getPromise()
           .then(...) 
           .then(...)
           .then(...)
            // .nodeify at the end of the chain, NOWHERE else
           .nodeify(callback)

}

第二方法是定义一个普通的回调api,然后只需调用 promisifyAll 。这很容易实现,实际上根本没有理由这么做,因为如果用户使用蓝鸟,用户可以很容易地自己宣传模块。

The second way is to define a normal callback api, and then just call promisifyAll. This is very easy to implement, in fact there is almost no reason to do it at all because the user can so easily promisify the module themselves if they use bluebird.

这篇关于使用bluebird创建支持节点回调和承诺的节点库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆