I have a larger use case that I narrowed down to the following code snippet.
This code might not make a lot of sense however this is just to demonstrate the issue I have.
I am trying to catch the exception thrown by a function (below called throwFunction).
I want to call the function but not wait for it to complete even though it returns a promise.
Directly after calling the function I call another async function (waitALittle).
I want to be able to catch any exception within the try catch.
The problem I see is that I cant seem to catch the thrown error and get unhandledRejection .
I tried surrounding almost every single piece of code with try catch but nothing works. I always get unhandled rejection.
Below is the output you see the unhandledRejection occurs in between the start and end of the waitFunction and that seems to be the core of the problem, when an exception is thrown while another function is still not resolved its promise.
questions.
- why is this happening, why doesnt the try catch block, catch the exception.
- How can this be fixed.
const setTimeout = require ('timers/promises').setTimeout;
async function waitALittle(){
await setTimeout(100);
}
async function throwFunction(a){
//do some stuff...
throw new Error('test');
}
async function f(){
try {
throwFunction();
console.log('wait started')
await waitALittle();
console.log('wait ended')
}catch(e) {
console.log('exception caught');
}
}
process.on('unhandledRejection', ()=>{
console.log('unhandledRejection event');
})
f();
Output:
wait started
“unhandledRejection event”
wait ended
I tried to cover many parts of the code with try catch but nothing caught the exception and I continued to get unhandledRejection.
ryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
For the try
/catch
to work on throwFunction
, you are missing await
in front of the call to throwFunction
:
async function f(){
try {
await throwFunction(); // <<< here
console.log('wait started')
await waitALittle();
console.log('wait ended')
}catch(e) {
console.log('exception caught');
}
}
Without it, the function runs asynchronously in parallel with the rest of the code. The error it throws results in the promise returned from the synchronous throwFunction()
call being rejected eventually. However you don’t use that promise for anything, and in particular no .catch
is called on it (not even implicitly using await
), so the rejection is unhandled.
Now, you said you don’t want to wait for it. In this case, you would need to handle exceptions on it separately using .catch
:
async function f(){
try {
throwFunction().catch(e => {
console.log('async exception caught');
});
console.log('wait started')
await waitALittle();
console.log('wait ended')
}catch(e) {
console.log('exception caught');
}
}
In this situation, being able to catch it within the outer try
would not even make sense as throwFunction
may throw after your other code isn’t even inside of the try
anymore.
If you just want to wait for both functions in parallel, though, so you continue once both are done, then it’s possible again, using Promise.all
:
async function f(){
try {
await Promise.all([
throwFunction(),
(async () => {
console.log('wait started')
await waitALittle();
console.log('wait ended')
})()
]);
}catch(e) {
console.log('exception caught');
}
}
(That async IIFE could be replaced with simply waitALittle()
if it weren’t for the console.log
statements.)