Wednesday 24 May 2017

Get Bluebird Promise from async await functions

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 !



via ncoden

No comments:

Post a Comment