Course contentsShow
← Web Development
Lesson 2274 of 2,98241. Debugging TechniquesFree lesson

Step Through Asynchronous Code

Debug promises, async/await, and callbacks by stepping through asynchronous execution flows correctly.

Step Through Asynchronous Code

What you'll learn: How to debug promises, async/await, and callbacks by stepping through asynchronous execution flows correctly in DevTools.

Why Async Code Breaks Traditional Stepping

When you hit a breakpoint inside a .then() callback, async function, or traditional callback, stepping normally won't follow the logical flow of your code. Instead, the debugger steps into browser internals, promise resolution machinery, or event loop mechanics—none of which help you understand your bug.

The analogy: Imagine debugging a conversation between two people passing notes. Traditional stepping would make you watch the mailman's entire route instead of just reading the next note's content.

How to Step Through Async Correctly

For promises and .then() chains:

  • Set breakpoints inside each .then() callback, not on the promise creation line
  • Use "Step Over" to stay within the current callback
  • The call stack will show the promise context

For async/await:

  • Place breakpoints on the lines after each await
  • "Step Over" will pause at the next await or line after the awaited promise resolves
  • The debugger automatically handles the suspension and resumption

For callbacks (setTimeout, event handlers):

  • Set breakpoints inside the callback function itself
  • Use event listener breakpoints or XHR breakpoints to catch when they fire
  • Check the call stack to see what triggered the callback

Key DevTools Features

Use async call stack tracking (enabled by default in modern DevTools) to see the full chain: where the async operation started → where it paused → where it resumed.

Key Takeaway: Debugging async code requires placing breakpoints inside callbacks and resumed contexts rather than on the initiating lines, and using DevTools' async stack tracking to follow the execution chain across time.