What await does
await is an operator that suspends the execution of an async function until a promise is fulfilled or rejected. After that, await either returns the promise result or throws an error, just like a plain throw.
Theory
TL;DR
awaitwaits for a promise to settle inside anasyncfunction.- If the promise fulfills,
awaitreturns its value. - If the promise rejects,
awaitthrows, andtry...catchcatches it. - It works only inside
asyncfunctions or as top-level await in modules. - It does not block the thread: other code keeps running while the function is paused.
- It also works with plain values: they are wrapped in
Promise.resolve(value).
Quick example
let result = await promise; // only inside an async function or top-level await
async function example() {
console.log('Before await');
const value = await Promise.resolve('Done');
console.log('After await:', value);
}
example();Syntax and what await returns
let result = await promise;It works only inside async functions (or in top-level await in modules).
Example 1, waiting for a promise
async function example() {
console.log('Before await');
const result = await Promise.resolve('Done');
console.log('After await:', result);
}
example();Output:
Before await
After await: Doneawait waits for the promise to settle and returns 'Done'.
Example 6, with non promise values. If you pass a plain value, it is immediately wrapped in Promise.resolve():
async function demo() {
const result = await 42;
console.log(result);
}
demo(); // 42This exists for uniformity: await always behaves as if a promise stood in front of it.
Example 7, nested await
async function inner() {
return 'Inside';
}
async function outer() {
const res = await inner();
console.log('Result:', res);
}
outer();Output:
Result: InsideSince inner() returns a promise, await simply waits for it to settle.
A rejection turns into an exception
Example 2, an error through await
async function fail() {
try {
const res = await Promise.reject('Failure!');
} catch (err) {
console.error('Caught:', err);
}
}
fail();Output:
Caught: Failure!If a promise is rejected, await throws an exception, which is why errors are conveniently caught with try...catch.
Sequential or parallel
Example 3, sequential execution
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:
1
(after 1 second)
2
(after 1 second)
3Each await waits for the previous promise, so the code runs sequentially.
Example 4, parallel execution (no immediate await)
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 finished');
}If you put await in front of each request, they run one after another instead of in parallel.
How await works inside the Event Loop
When the engine meets await promise, it:
- suspends execution of the current
asyncfunction; - registers the continuation (the rest of the function body) as a microtask;
- frees the main thread, so other code can run;
- when the promise settles, the microtask runs the rest of the function.
That is why await does not "block" JS, it only postpones the continuation of the function until the promise is ready.
Example 5, a "pause" without blocking
async function wait() {
console.log('Start');
await new Promise(res => setTimeout(res, 2000));
console.log('End');
}
wait();
console.log('This code runs while the waiting is going on!');Output:
Start
This code runs while the waiting is going on!
(after 2 seconds)
Endawait does not block the thread, only the function itself.
Summary
Think of
awaitas a pause at a traffic light:
- the light is red (
Promise pending), you wait;- green (
fulfilled), you drive on;- a crash (
rejected), an error is thrown and you move tocatch.
| Feature | Description |
|---|---|
| What it does | suspends the async function until the promise settles |
| Returns | the resolve value, or throws on reject |
| Where it can be used | only inside async functions (or top-level await in modules) |
| Does not block | the rest of the program keeps running |
| Works with | promises and plain values (Promise.resolve(value)) |
Common mistakes
- Believing
awaitblocks the page. It pauses only its own function, not the thread: timers, event handlers and the rest of the code keep running. awaitinside a loop for independent requests. Every iteration waits for the previous one; independent operations belong inPromise.all.- Not wrapping
awaitintry...catch. A rejected promise throws, and without handling it becomes an unhandled rejection. awaitoutside anasyncfunction in a classic script: that is a SyntaxError. Top-level await is available only in modules.- Putting
awaitin front of everyfetchwhen the requests are independent: you are deliberately making the code slower.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.