Description
A Rejected Promise
returned by an async
function will not be caught by the function's try-catch
block. This may surprise some users and lead to unexpected behavior:
(async () => {
try {
// will result in an UnhandledPromiseRejection
return Promise.reject('Error!');
} catch (e) {
// will not run
handleCleanup();
}
})();
conversely, await
-ed promises work in a more intuitive fashion:
(async () => {
try {
// will not result in an UnhandledPromiseRejection
return await Promise.reject('Error!');
} catch (e) {
// will run
handleCleanup();
}
})();
I understand why this is the case according to the async-await
function spec, but it feels like confusing behavior and a problem that typescript-eslint
could help avoid via the following rule:
disallow return
-ing a Promise
from inside an async
function try-catch
block.
Thus, the first case described above would be invalid according to this rule, but the second case would be valid. We could even, potentially, provide an auto-fix that would add the await
statement to the promise.
There's also the possibility for the broader rule of disallowingreturn
-ing a Promise
from anywhere inside an async
function, but I wanted to start with the narrowest case.
Please let me know if there's any other clarifying information I can provide, thanks so much for your time!