Get Bluebird Promise from async await functions

后端 未结 1 1138
梦如初夏
梦如初夏 2020-12-06 01:37

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:

相关标签:
1条回答
  • 2020-12-06 02:11

    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 functions 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(…);
    
    0 讨论(0)
提交回复
热议问题