The following code
const run = async() => {
try {
const p = Promise.reject()
p.catch(() => console.log('exception caught'))
await p
console.log('flow completed')
} catch {
console.log('flow interrupted')
}
}
run()
Prints the following output:
exception caught
flow interrupted
Meaning, even though the catch method is running the exception is not caught
Now if i make this seemingly irrelevant edit and chain the catch right after the promise definition
const run = async() => {
try {
const p = Promise.reject().catch(() => console.log('exception caught'))
await p
console.log('flow completed')
} catch {
console.log('flow interrupted')
}
}
run()
the output becomes
exception caught
flow completed
I tried defining the promise like
const p = new Promise((resolve, reject) => {setTimeout(reject, 100)})
thinking it might matter if the catch was set before the promise rejection but it did not change anything
Im running these tests with node 12.16.1
Can anyone explain why is the exception not caught in the first code sample, but is caught in the second ?