Skip to content
BENIBIX Learn

Async/Await

Development🧭 Programming ParadigmsAsynchronous Code

Also known as async await, async, await

Async code that reads top to bottom. await waits for each result.

Think of it like

Think of it like a recipe that says wait for the oven. You read it top to bottom, like any recipe.

Example

Emma writes const data = await fetch(url). The next line can use data, and the page never freezes while it waits.

Why it matters

It is the most common way to write async code today. It is far easier to read than chains of callbacks.

Definition

Async/await lets you write async code that reads like normal step-by-step code. You mark a function with async, then put await in front of a slow call. At an await, the function pauses until the result is ready, while the rest of the program keeps running. Errors are caught with a plain try and catch. JavaScript, Python, C# and Rust all have it.

Example code

javascript
async function load() {
  const res = await fetch("/api/user");
  return await res.json();
}
Each await waits for its step, and the code still reads in order.

Common mistake

Putting await on each call in a row when they do not depend on each other. Start them together with Promise.all.

Key takeaways

  1. 1
    Reads in order. Like normal code.
  2. 2
    await pauses. Only this function.
  3. 3
    Built on promises. Same thing underneath.

Compare

Learn it properly