bluebird: returning Promise.reject in async function results in Unhandled rejection error message
-
What version of bluebird is the issue happening on? 3.5.0
-
What platform and version? (For example Node.js 0.12 or Google Chrome 32) Node.js 8.0.0 on Ubuntu
-
Did this issue happen with earlier version of bluebird? Yes
When returning Promise.reject in async function, an error is printed on the console “Unhandled rejection” even the promise is caught. It doesn’t print out the error if native Promise is used.
const Promise = require("bluebird");
async function WaitAsync(){
return Promise.reject(new Error("reject"));
}
Promise.resolve().then(() => {
return WaitAsync();
}).catch(err => {
console.log("caught: ", err);
});
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 14
- Comments: 21 (2 by maintainers)
I believe I’m encountering another instance of this issue. In fact I was going to post a new issue before I saw this one. Here’s my description:
This issue is a bit difficult to grasp, so I’ll do my best to explain with examples. It seems that when using
Promise.using()
with an async function, awaiting on already rejected promises always outputs anUnhandled rejection
when using Bluebird’sPromise.reject()
, but not when using nativePromise.reject()
orthrow
in an async function.These code snippets are runnable directly on node 8 (tested on v8.1.0) with Bluebird v3.5.0 (and earlier).
First, let’s start from a problematic example.
This snippet, when run, produces the following output:
As you can see, we have an unhandled rejection error yet we did handle the error, and no clear way of rewriting the snippet so that the unhandled rejection would not occur.
Confusingly, if we reject by using native promises, we do not get the unhandled rejection. In this snippet, we only use
.using()
from Bluebird.This outputs simply:
Similarly, this also prints the warning:
However, this one doesn’t:
And neither does this one:
With async/await translated to plain promises, the failing examples also work without issue:
This leads me to believe that there may perhaps be a bug in Bluebird, causing a rejected bluebird-promise returned by an async function to be handled asynchronously.
Thoughts?
We just got bit by this issue. We’ve just recently moved to node@8.2.1 and are using async/await extensively, along with bluebird.
I see @suguru03 has a PR to fix it, is that going to be merged soon?
This change causes resource leak check to fail in our tests
@adamreisnz I believe there’s no way to alter the type of Promise returned from async functions, it is always internal
Promise