← All concepts
Asynchronous JavaScript

Async/Await

await pauses an async function until a promise settles, without blocking the rest of the program.

Loading visual…

Definition

The `async` keyword turns a function into one that always returns a promise; whatever it returns becomes the fulfillment value, and a thrown error becomes the rejection reason. Inside an async function, the `await` keyword can be placed in front of a promise, or any thenable, to pause that function's execution until the promise settles. Pausing only affects that one function's frame, not the whole program: control returns to whatever called the async function while the awaited promise is still pending, so other code keeps running normally. When the awaited promise settles, the async function's frame resumes exactly where it left off. If the promise fulfilled, await evaluates to the fulfillment value, and if it rejected, await throws that rejection reason, which a surrounding try/catch can catch. async/await is built on top of promises, it is syntax that lets promise-based code read like sequential, synchronous code, but it does not change the underlying scheduling: resuming an async function after an await still happens as a microtask, in the same event loop that runs .then() callbacks.

Examples

async function getUser(id) {
  const response = await fetch(`/api/users/${id}`);
  const user = await response.json();
  return user;
}

Each await pauses getUser's frame until the fetch and then the JSON parsing settle; the calling code was free to keep running while getUser was paused.

async function safeFetch(url) {
  try {
    const res = await fetch(url);
    return await res.json();
  } catch (err) {
    console.log("request failed:", err.message);
    return null;
  }
}

If the awaited fetch or json() call rejects, await throws that rejection inside safeFetch, and the try/catch handles it just like a synchronous error.

Where you see this

Practice this

Further reading