I've got 2 promises in a try/catch block, with a third function afterwards using the awaited responses.
When I await
res1 and res2 within the parameters of lastFunction, the error that is thrown is not caught by the catch block.
When I await them normally by const res2 = await promise2()
, the error is caught in the catch block.
Why is this happening?
const handler = async () => {
const results = await Promise.all(
[1].map(async id => {
try {
const res1 = promise1()
const res2 = promise2()
const finalResponse = await lastFunction(await res1, await res2)
} catch (err) {
console.log('Catching error')
console.log(err)
}
})
)
}
const promise1 = () => {
return new Promise((res, rej) => {
setTimeout(() => res(1), 1000)
})
}
const promise2 = () => {
return new Promise((res, rej) => {
throw new Error('second one broke')
})
}
const lastFunction = (prom1, prom2) => {
return new Promise((res, rej) => {
res('done')
})
}
handler();
CodePen: https://codepen.io/OliverNural/pen/oNbrMWw (Open console)