Guide
Diagnosing Unhandled Promise Rejections in async/await Code
Learn how to spot, diagnose, and fix unhandled promise rejections in async/await code across browsers and Node.js.
Published by Tasadduq Burney
30 Oct 2025, 00:09 UTC
2 min39.3K views0

Recognizable condition
When running JavaScript code that uses async/await, you see a warning like “UnhandledPromiseRejectionWarning” in the browser console or Node.js prints “(node:xxxx) UnhandledPromiseRejectionWarning” and may exit with code 1 (Node 15+).
Cause / diagnostic table
| Observed symptom | Likely cause |
|---|---|
Warning appears after an await expression | The awaited promise rejected and no try/catch surrounds it |
Warning appears at script start, no await in sight | A top‑level await or a returned rejected promise from an async function bubbles up |
| Warning only in Node.js, silent in browser | Node 15+ enables unhandledrejection by default; older versions may ignore it |
Ordered checks
- Locate every
asyncfunction that contains anawaitwithout a surroundingtryblock. - For each located function, verify whether the awaited promise can reject (e.g., it calls a network API, file system, or returns
Promise.reject). - Check if the function returns a promise that is used elsewhere without
.catchorawaitin a try/catch. - In Node.js, run
node --trace-warnings script.jsto see the full stack; in browsers, open the console and look for the warning. - Confirm the Node version:
node -v. Versions ≥15 enable the warning; <15 may hide it.
Fixes tied to findings
- Missing try/catch – wrap the
awaitintry { await expr; } catch (e) { /* handle */ }. - Returned rejected promise – either
return await expr;inside a try/catch or add.catchon the returned promise where it is used. - Top‑level await – if using ES modules, add a
try/catcharound the top‑level await or listen toprocess.on('unhandledRejection')in Node. - Global handler missing – attach a listener:
process.on('unhandledRejection', (reason, promise) => { console.error('Unhandled', reason); process.exit(1); })(only after fixing the root cause).
Escalation criteria
If after applying the above checks the warning persists:
- Enable detailed tracing:
node --trace-process-warnings --trace-unhandled-rejections script.js. - Consider using a wrapper library (e.g.,
p‑limitorasync‑hooks) to centralize error handling. - If the code runs in both browser and Node, verify that the environment‑specific global (
windowvsprocess) is handled appropriately. - As a last resort, upgrade Node to a supported LTS version (≥18) where the warning behavior is well‑documented.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.