I am looking for a way, with Node v7.6 or above, to get a Bluebird Promise (or any non-native promise) when an async function is called.
In the same way I can do:
global.Promise = require('Bluebird'); // Or Q/When
var getResolvedPromise = () => Promise.resolve('value');
getResolvedPromise
.tap(...) // Bluebird method
.then(...);
See: May I use global.Promise=require("bluebird")
I want to be able to do something like:
global.Promise = require('Bluebird'); // Or Q/When
var getResolvedAsyncAwaitPromise = async () => 'value';
getResolvedAsyncAwaitPromise()
.tap(...) // Error ! Native Promises does not have `.tap(...)`
.then(...);
I know I can use at any moment something like:
Bluebird.resolve(getResolvedAsyncAwaitPromise())
.tap(...);
But I was curious if there would be a way to change the default Promise returned by AsyncFunction
. The constructor seems enclosed:
Note that AsyncFunction is not a global object. It could be obtained by evaluating the following code.
Object.getPrototypeOf(async function(){}).constructor
MDN reference on AsyncFunction
If there is no way to change the AsyncFunction
's Promise constructor, I would like to know the reasons of this locking.
Thank you !
Integration With Async/Await Unfortunately there is no way to get async functions to return Bluebird promises. Even if you set global. Promise = require('bluebird'); , async functions will still return native promises.
The behavior of async / await is similar to combining generators and promises. Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.
Async/Await is used to work with promises in asynchronous functions. It is basically syntactic sugar for promises. It is just a wrapper to restyle code and make promises easier to read and use. It makes asynchronous code look more like synchronous/procedural code, which is easier to understand.
Bluebird JS is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to “promisify” other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions.
Is there a way to change the default Promise returned by AsyncFunction
No.
What are the reasons of this locking
The ability to hijack all async function
s could be a security issue. Also, even where that is no problem, it's still not useful to do this replacement globally. It would affect your entire realm, including all libraries that you are using. They might rely on using native promises. And you cannot use two different promise libraries, although they might be required.
I want to be able to do something like:
getResolvedAsyncAwaitPromise().tap(...)
What you can do is to wrap the function at its definition with Promise.method
:
const Bluebird = require('Bluebird');
const getResolvedAsyncAwaitPromise = Bluebird.method(async () => 'value');
getResolvedAsyncAwaitPromise()
.tap(…) // Works!
.then(…);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With