Testing & Debugging

Debugging Async Code

Async bugs are among the hardest to find — silent rejections, race conditions, missing awaits, and broken stack traces. Learn the patterns, pitfalls, and tools to track down even the sneakiest async bugs.

How do you debug asynchronous JavaScript?

Use async/await so stack traces stay readable, put breakpoints on await lines and step over them in DevTools, and enable "Pause on caught exceptions" when an error is swallowed. Log with timestamps to see ordering problems, check the Network tab for failed requests, and add a global unhandledrejection listener during development.

window.addEventListener("unhandledrejection", (e) => {
  console.error("Unhandled:", e.reason);
});

console.time("load");
await loadData();
console.timeEnd("load");

Top Async Bug Patterns

forEach + async

❌ Wrong

ids.forEach(async id => await fetch(id))

✅ Correct

for (const id of ids) { await fetch(id); } // sequential await Promise.all(ids.map(fetch)); // parallel

forEach ignores returned Promises — callbacks run but are never awaited

Missing await on .json()

❌ Wrong

const data = fetch('/api').json(); // data is a Promise!

✅ Correct

const res = await fetch('/api'); const data = await res.json();

.json(), .text(), .blob() all return Promises that must be awaited

Promise constructor wrapping async

❌ Wrong

return new Promise(async (resolve) => { resolve(await getData()); });

✅ Correct

return getData(); // already returns a Promise — no wrapper needed

Async inside Promise constructor can silently swallow rejections

Async constructor

❌ Wrong

class Svc { constructor() { this.data = await load(); } }

✅ Correct

class Svc { static async create() { const s = new Svc(); await s.init(); return s; } }

Constructors are synchronous — use a static factory method instead

Common Pitfalls

Catching errors silently with an empty catch block — the bug disappears, not fixed
Always log or re-throw: catch(err) { logger.error(err); throw err; } Never swallow errors silently.
Using Promise.race() when you want all results — only gets the first one
Use Promise.all() for all results (fails fast), Promise.allSettled() to get all results including failures.
Not setting up global unhandledRejection handlers — Node.js crashes without warning
process.on('unhandledRejection', ...) in Node.js; window.addEventListener('unhandledrejection', ...) in browser.
Using async/await without try/catch in Express routes — unhandled rejection crashes server
Wrap route handlers in an asyncHandler(fn) utility that calls next(err) on any rejection.

Interactive Demo

Code Editor

<button id="bug">Missing await (bug)</button>
<button id="fix">With await (fixed)</button>
<pre id="out"></pre>

Preview

Debugging Async Code — step by step

1 / 4
1

The Async Call Stack Problem

When an async operation completes, its callback runs in a new call stack. DevTools shows the continuation stack, not the original call site. Enable async stack traces in DevTools (Settings > Experiments) to see the full chain.

1 of 4
💡Use console.trace to See Who Called What
When you are unsure where a particular async call originates, add console.trace inside the function. It prints the full call stack at that moment — including async frames when stack trace capture is enabled — helping you trace the request back to its trigger.

🎯 Quick Check

Why does a rejected Promise not always show an error in the console?

Debugging Async JavaScript: summary

Key points

  • Race conditions: a slow, older request can finish after a newer one. Cancel stale requests with AbortController.
  • A missing await is a common bug: you get a Promise object instead of the value.
  • DevTools shows async stack traces across await boundaries.

Frequently asked questions

›Why do I see [object Promise] or Promise {<pending>}?

You used the result of an async function without await. Add await, or use .then().