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)); // parallelforEach 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 neededAsync 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
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
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.
🎯 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
awaitis a common bug: you get a Promise object instead of the value. - DevTools shows async stack traces across
awaitboundaries.
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().