What does await do?
Short definition
awaitis an operator that pauses the execution of anasyncfunction until the promise either fulfills or rejects.After that:
- if the promise is fulfilled,
awaitreturns its result;- if the promise is rejected,
awaitthrows an error (likethrow).
Syntax
let result = await promise;It works only inside asyncfunctions (or as 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 resolve and returns 'Done'.
Example 2 - an error through await
async function fail() {
try {
const res = await Promise.reject('Error!');
} catch (err) {
console.error('Caught:', err);
}
}
fail();Output:
Caught: Error!If the promise is rejected, await throws an exception,
so errors are easy to catch with try...catch.
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 sec)
2
(after 1 sec)
3Each await waits for the previous promise to finish,
so the code runs sequentially.
Example 4 - parallel execution (no await inside)
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:
- It pauses execution of the current
asyncfunction. - It registers the continuation (the remaining code) as a microtask.
- It frees up the main thread (other code can run).
- 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
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:
Start
This code runs while the wait is happening!
(after 2 sec)
Endawait 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():
async function demo() {
const result = await 42;
console.log(result);
}
demo(); // 42This is needed for uniformity - await always behaves as if it is dealing with a promise.
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 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 intocatch.
Summary
| Feature | Description |
|---|---|
| What it does | Pauses the async function until the promise settles |
| Returns | The resolve result, or throws an error 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's code keeps running |
| Works with | Promises and plain values (Promise.resolve(value)) |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.