Digiaru

Digiaru started this conversation 1 week ago.

Unexpected Promise Rejection Not Caught in Async Function

I'm running multiple asynchronous calls in an async function. Even though I wrap them in try/catch, some promise rejections still escape and become unhandled.

Kar

Posted 1 week ago

In this pattern, errorPromise() rejects before the await block, so it's not handled within the async try/catch. JavaScript treats promises created without immediate await as separate tasks.Reddit+15Reddit+15Reddit+15 How to fix: • Immediately await each promise: js Copy code const p2 = await errorPromise(); • Or use Promise.all(): js Copy code await Promise.all([p, p2]); Optionally, attach early .catch() to any promise to mark it handled: js Copy code const p2 = errorPromise().catch(err => console.error('Handled early', err)); But beware of silent swallows.PixelFreeStudio Blog -+7Reddit+7Just Program It+7DEV Community+2Reddit+2InfiniteJS+2The Linux Code