await Suspends, It Does Not Block
An async function returns at its first await and continues later, which is the one fact that makes async output predictable.
async marks a function as always returning a promise. await unwraps one, and in doing so suspends the function and schedules the rest as a microtask. Nothing is blocked; the caller carries on immediately.
EXERCISE
1async Always Returns a Promise
The keyword changes the return value of a function whatever its body does, and that is most of what it means.
async does two things. It allows await inside the function, and it wraps the return value in a promise. The second one is easy to overlook and explains several surprises.
Whatever you return comes back wrapped:
async function getNumber() {
return 42;
}
const result = getNumber();
console.log(result); // Promise { 42 }
console.log(result === 42); // false
getNumber().then((n) => console.log(n)); // 42
console.log(await getNumber()); // 42
There is no await anywhere in that function and nothing asynchronous happens inside it. It still returns a promise, because async is on it.
A throw becomes a rejection:
async function fail() {
throw new Error("nope");
}
fail().catch((err) => console.log(err.message)); // nope
So an async function never throws to its caller synchronously. It returns a promise that rejects, and the caller has to handle it with catch or try/await.
Returning a promise does not double-wrap it:
async function getUser() {
return fetch("/api/user"); // already a promise
}
const p = getUser();
console.log(p instanceof Promise); // true, one layer not two
The same flattening as .then from Unit 5: a returned promise is waited for rather than nested.
Which makes async and .then two spellings of one thing:
// These are equivalent:
async function a() {
return 1;
}
function b() {
return Promise.resolve(1);
}
That equivalence is worth holding on to. Anywhere you can reason about a .then chain, you can reason about the async version by translating it, and Unit 5's rules carry over unchanged.
One consequence worth stating plainly:
async function save(data) {
await writeToDisk(data);
}
save(data); // fire and forget: nobody sees a failure
await save(data); // failure surfaces here
save(data).catch(handle); // failure is handled
Calling an async function without awaiting it or attaching a catch means any rejection becomes an unhandled rejection. Nothing stops you, and in it will eventually terminate the process.
Read async as "this function returns a promise", not as "this function is slow" or "this function does something in the background". A function with no await in it is still async in the only sense that matters to a caller, and that is precisely why it is a useful marker on an interface.
EXERCISE
2What await Actually Does
It is not a pause. The function returns, and the remainder is scheduled to run later.
This is the sentence worth getting exactly right: await suspends the function and schedules the rest of it as a microtask. It does not block anything.
Watch the function return before it finishes:
async function run() {
console.log("1: start");
await null;
console.log("3: after await");
}
console.log("0: before call");
run();
console.log("2: after call");
// 0: before call
// 1: start
// 2: after call
// 3: after await
Read the order carefully. Lines 0 and 1 ran normally. At await null the function handed control back to the caller, which printed line 2. Only when the stack emptied did the remainder resume and print line 3.
await null waits for nothing meaningful. There is no delay involved. The suspension is not about waiting, it is about yielding.
Which is why this is the equivalent .then version:
function run() {
console.log("1: start");
return Promise.resolve(null).then(() => {
console.log("3: after await");
});
}
Everything after the await is the callback. Once you see that, the ordering stops needing explanation: it is the microtask rule from Unit 4.
An async function runs synchronously until its first await:
async function work() {
console.log("A"); // synchronous
console.log("B"); // synchronous
await something(); // suspends here
console.log("C"); // microtask
await somethingElse(); // suspends again
console.log("D"); // another microtask
}
console.log("before");
work();
console.log("after");
// before
// A
// B
// after
// C
// D
Every await is another suspension point, and every resumption is another microtask.
And a common wrong prediction, worth testing yourself on:
async function first() {
console.log("1");
await second();
console.log("4");
}
async function second() {
console.log("2");
}
first();
console.log("3");
// 1
// 2
// 3
// 4
second() ran fully and synchronously, printing 2. Then first suspended at the await, letting 3 print, and resumed for 4. Reading await as "wait here" predicts 1 2 4 3, which is wrong.
Pro Tip: When async output confuses you, mentally rewrite every
awaitas.then(() => { rest of the function }). The two are the same scheduling with different syntax, and the.thenform makes the yield point visible, which is exactly the thing the ordering depends on.
EXERCISE
3What await Accepts
await works on any value, not only on promises, and what it does with a non-promise is worth knowing exactly.
await does not require a promise. It accepts anything, and the rule for what happens is short.
A non-promise is returned as-is, after one turn:
async function run() {
const a = await 42;
const b = await "hello";
const c = await null;
console.log(a, b, c); // 42 hello null
}
No error, no conversion. The value comes straight back. What it still does is suspend, which is why await null in the last exercise changed the ordering despite waiting for nothing.
Which means an unnecessary await is not free:
async function getName(user) {
return await user.name; // user.name is a string
}
async function getNameBetter(user) {
return user.name; // no suspension
}
Both produce the same value. The first costs a microtask turn for nothing. It rarely matters, and it matters inside a loop that runs ten thousand times.
await also works on a thenable, which is anything with a then method:
const thenable = {
then(resolve) {
resolve("from a thenable");
},
};
async function run() {
const value = await thenable;
console.log(value); // from a thenable
}
This is not a curiosity. It is how promise interoperability works: await and Promise.resolve both call .then on whatever they are given, so an object from an older library that implements then works with modern syntax without any adapter.
And it explains a genuinely confusing bug:
class Result {
constructor(value) {
this.value = value;
}
then(resolve) {
resolve(this.value);
}
}
async function run() {
const result = await new Result(42);
console.log(result); // 42, not the Result object
}
An ordinary class that happens to have a method called then cannot be awaited or returned from an async function without being unwrapped. If you name a method then on a class you also return from async code, you have accidentally made it a thenable. This is rare and extremely puzzling when it happens.
One more case: awaiting in a non-async function is a syntax error, not a runtime one:
function notAsync() {
// const x = await something();
// SyntaxError: await is only valid in async functions
}
The code does not run at all, which is the helpful outcome.
