从asynchronous获取蓝鸟承诺等待function

我正在寻找一种方法,使用Node v7.6或更高版本,在调用asynchronous函数时获取Bluebird Promise(或任何非本地promise)。

我可以这样做:

global.Promise = require('Bluebird'); // Or Q/When var getResolvedPromise = () => Promise.resolve('value'); getResolvedPromise .tap(...) // Bluebird method .then(...); 

请参阅: 我可以使用global.Promise = require(“bluebird”)

我希望能够做到这样的事情:

 global.Promise = require('Bluebird'); // Or Q/When var getResolvedAsyncAwaitPromise = async () => 'value'; getResolvedAsyncAwaitPromise() .tap(...) // Error ! Native Promises does not have `.tap(...)` .then(...); 

我知道我可以随时使用这样的东西:

 Bluebird.resolve(getResolvedAsyncAwaitPromise()) .tap(...); 

但是我很好奇,是否有办法改变AsyncFunction返回的默认Promise。 构造函数似乎包含在内:

请注意,AsyncFunction不是全局对象。 可以通过评估下面的代码来获得。

 Object.getPrototypeOf(async function(){}).constructor 

AsyncFunction MDN引用

如果没有办法改变AsyncFunction的Promise构造函数,我想知道这个locking的原因。

谢谢 !

有没有办法改变由AsyncFunction返回的默认Promise

没有。

这个locking的原因是什么?

劫持所有async function的能力可能是一个安全问题。 而且,即使在没有问题的情况下,在全球范围内进行replace也是没有用的。 它会影响你的整个领域,包括你正在使用的所有库。 他们可能依靠使用本地的承诺。 你不能使用两个不同的承诺库,尽pipe他们可能是必需的。

我希望能够做到这样的事情:

 getResolvedAsyncAwaitPromise().tap(...) 

可以做的是用Promise.method定义函数的定义:

 const Bluebird = require('Bluebird'); const getResolvedAsyncAwaitPromise = Bluebird.method(async () => 'value'); getResolvedAsyncAwaitPromise() .tap(…) // Works! .then(…);