Course contentsShow
← JavaScript
Lesson 1392 of 2,72330. Async/AwaitFree lesson

Assuming Synchronous Execution Order

Learn that await doesn't make code before the awaited expression execute synchronously with code after.

Assuming Synchronous Execution Order

What You'll Learn

You'll learn why await doesn't magically turn asynchronous code into truly synchronous code, and how execution order can still surprise you.

The Core Misconception

Many developers assume that because await makes async code look synchronous, everything in an async function executes in a simple top-to-bottom order. This isn't true. While await pauses execution within that specific async function, other code continues running elsewhere.

Understanding the Execution Flow

When you use await, you're telling JavaScript: "pause this function until the promise settles." But you're not pausing the entire JavaScript runtime. Other functions, event handlers, timers, and async operations continue executing normally.

async function misleading() {
  console.log('1: Start');
  await Promise.resolve();
  console.log('3: After await');
}

misleading();
console.log('2: Outside async function');

// Output:
// 1: Start
// 2: Outside async function
// 3: After await

Notice that line 2 executes before line 3, even though line 3 appears earlier in the file. The await causes the rest of misleading() to be scheduled on the microtask queue, allowing synchronous code outside the function to finish first.

Why This Matters

This becomes critical when you have multiple async functions running concurrently, or when mixing async and sync operations:

async function first() {
  console.log('A');
  await delay(10);
  console.log('B');
}

async function second() {
  console.log('C');
  await delay(5);
  console.log('D');
}

first();
second();
// Output: A, C, D, B (not A, B, C, D!)

Key Takeaway: await only pauses the current async function, not the entire program. Code outside the function and other async operations continue executing, which can create unexpected execution orders if you assume everything is truly synchronous.