首页 文章

将承诺转换为蓝鸟

提问于
浏览
45

我找到了一个使用promises的现有库,但它并没有提供bluebird的所有额外功能 .map().tap() . 我如何将"normal"或"non-bluebird"承诺转换为蓝鸟承诺,蓝鸟提供的所有额外功能?

我尝试在 Promise.promisifyPromise.resolve 中包装现有的承诺,但似乎都没有效果 .

3 回答

  • 70

    使用Promise.resolve - 它将采取任何可能的,如来自其他实现的承诺,并将其同化为Bluebird承诺 .

    请记住,the term "resolve"可能会产生误导,它并不意味着与"fulfill"相同,但也可以遵循另一个承诺并解决其结果 .

  • 15

    如果你想将promise转换为bluebird promise什么都不解决并返回 customPromise 那么你就可以访问链中的所有bluebirds自定义方法 .

    Promise.resolve().then(function(){
      return customPromise()
    })
    

    要么

    Promise.resolve(customPromise())
    
  • 1

    使用Bluebird的 Promise.method

    const Promise = require('bluebird');
    
    const fn = async function() { return 'tapped!' };
    
    bluebirdFn = Promise.method(fn);
    
    bluebirdFn().tap(console.log) // tapped!
    fn().tap(console.log) // error
    

相关问题