Node.js · 03/19 6 min
Async pitfalls catalogue
Every async bug that shows up in debugging rounds, with the one-line fix.
| Bug | Symptom | Fix |
|---|---|---|
Missing await |
value is Promise { <pending> }, checks always truthy |
await it |
forEach(async…) |
function returns before work finishes | for…of + await, or Promise.all(map) |
| Sequential awaits | correct but slow | Promise.all independent calls |
Promise.all fail-fast |
one rejection loses all results | Promise.allSettled when partial success is OK |
Unawaited return inside try |
catch never sees the rejection | return await p inside try/catch |
| Timer leak | test hangs / never exits | clearTimeout, clearInterval in cleanup |
| Shared mutable state | flaky under concurrency | atomic DB ops, locks, or per-request state |
| Retry without backoff | thundering herd | exponential backoff + jitter, max attempts |
async function getUser(id) {
try {
return await db.users.findById(id); // 'await' here makes the catch work
} catch (e) {
throw new NotFound(id);
}
}
Next in Node.js
JavaScript gotchas