Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

Why await Doesn't Make JavaScript Run in Order

A lot of developers learn async / await and walk away with one idea: I used await , so everything waits. That sentence is almost true — and the "almost" is where the bugs live. await pauses one async function . It does not pause the rest of your program. Timers still fire. Event handlers still run. Other async functions keep moving. JavaScript is still taking turns. Once you see that, a lot of…

Many developers learn async/await, then assume that every async function will pause the entire program until it completes. This assumption is almost true, but it falls apart due to one crucial detail. The `await` keyword only pauses the specific async function it is called within; it does not halt the rest of the program. JavaScript continues to run timers, execute event handlers, and proceed with other async functions. Understanding this fundamental behavior can explain many puzzling "why did this log first?" moments.

The common mental model that people get wrong is: `await doSomething(); // nothing else in the entire program can happen until this finishes`. While this statement may seem convenient, it is not how JavaScript works. `await` is local to the function it is used in and only instructs that function to pause at that line, returning control to the caller once the awaited Promise settles. Other parts of the program can continue running uninterrupted.

To illustrate this concept, consider a simple example involving two async functions, `loadUser()` and `loadSettings()`, each using `await` with a `setTimeout`-like function called `later()`. When these functions are called sequentially within an asynchronous context, the output order reflects the actual execution rather than the order of the function definitions. The key takeaway here is that `await` does not freeze the program; it merely pauses the current async function, allowing other tasks to proceed.

When multiple `await` statements are chained, such as `await loadUser(); await loadSettings();`, the functions run sequentially, with the caller of the first function having to wait for it to complete before proceeding. However, if multiple `await` calls are made without chaining, like `await loadUser(); loadSettings();`, the JavaScript engine continues to execute other tasks, like timers and network requests, while the functions are waiting.

This means that the order of completion is determined by the actual execution time of each function, not by their sequential declaration.

In practical applications, `await` is particularly useful when the outcome of one function is needed by another. For instance, fetching user data before loading their projects ensures that the application logic works correctly. However, when tasks are independent, using `Promise.all()` can be more efficient, as it allows JavaScript to handle multiple requests concurrently, thereby reducing overall wait time.

The choice between using sequential `await` calls and `Promise.all()` depends on whether the tasks are dependent on each other or can be executed in parallel.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Cipr and Ciprnode zero

The Cipr (Cosmic Index of Public Resources) is a decentralized, distributed, and censorship-resistant web index where domain owners control their own entries.

Sidewalk to Summit — build log

A note before this starts moving: Been quiet here for a few weeks — heads down building instead of writing about it, which is exactly the trap this devlog exists to prevent.

More from Sunday 16 August →