Skip to main content

What does await do?

Short definition

await is an operator that pauses the execution of an asyncfunction until the promise either fulfills or rejects.

After that:

  • if the promise is fulfilled, await returns its result;
  • if the promise is rejected, await throws an error (like throw).

Syntax

javascript
let result = await promise;

It works only inside asyncfunctions (or as top-level await in modules).

Example 1 - waiting for a promise

javascript
async function example() { console.log('Before await'); const result = await Promise.resolve('Done'); console.log('After await:', result); } example();

Output:

javascript
Before await After await: Done

await waits for the promise to resolve and returns 'Done'.

Example 2 - an error through await

javascript
async function fail() { try { const res = await Promise.reject('Error!'); } catch (err) { console.error('Caught:', err); } } fail();

Output:

javascript
Caught: Error!

If the promise is rejected, await throws an exception, so errors are easy to catch with try...catch.

Example 3 - sequential execution

javascript
async function steps() { console.log('1'); await new Promise(res => setTimeout(res, 1000)); console.log('2'); await new Promise(res => setTimeout(res, 1000)); console.log('3'); } steps();

Output:

javascript
1 (after 1 sec) 2 (after 1 sec) 3

Each await waits for the previous promise to finish, so the code runs sequentially.

Example 4 - parallel execution (no await inside)

javascript
async function parallel() { const a = fetch('/api/user'); const b = fetch('/api/posts'); // Both run at the same time const [user, posts] = await Promise.all([a, b]); console.log('Both requests completed'); }

If you put await before each request, they will run one after another, not in parallel.

How await works inside the Event Loop

When the engine encounters await promise:

  1. It pauses execution of the current async function.
  2. It registers the continuation (the remaining code) as a microtask.
  3. It frees up the main thread (other code can run).
  4. When the promise settles, the microtask runs the remaining code.

That is why await does not "block" JS; it just defers the continuation of the function until the promise is ready.

Example 5 - a "pause" without blocking

javascript
async function wait() { console.log('Start'); await new Promise(res => setTimeout(res, 2000)); console.log('End'); } wait(); console.log('This code runs while the wait is happening!');

Output:

javascript
Start This code runs while the wait is happening! (after 2 sec) End

await does not block the thread, only the function itself.

Example 6 - with non-promises

If you pass a plain value, it is immediately wrapped in Promise.resolve():

javascript
async function demo() { const result = await 42; console.log(result); } demo(); // 42

This is needed for uniformity - await always behaves as if it is dealing with a promise.

Example 7 - nested await

javascript
async function inner() { return 'Inside'; } async function outer() { const res = await inner(); console.log('Result:', res); } outer();

Output:

javascript
Result: Inside

Since inner() returns a promise, await just waits for it to finish.

A simple analogy

Think of await as a pause at a traffic light:

  • When it turns red (Promise pending), you wait.
  • When it turns green (fulfilled), you continue on your way.
  • When there is a crash (rejected), an error is thrown and you move into catch.

Summary

FeatureDescription
What it doesPauses the async function until the promise settles
ReturnsThe resolve result, or throws an error on reject
Where it can be usedOnly inside async functions (or top-level await in modules)
Does not blockThe rest of the program's code keeps running
Works withPromises and plain values (Promise.resolve(value))

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.