`async` / `await`
async marks a function that returns a promise. await pauses that function until a promise settles, then resumes with the value or throws the rejection.
Detailed Explanation
async/await is syntax over promises, not a second concurrency model. async function load() { return 1; } returns a promise that fulfills with 1. Throwing inside load rejects that promise. The caller still uses .then or await.
await expr does three things. It converts expr with Promise.resolve. It yields the async function, so the caller continues (or the current task ends). When the promise fulfills, the function resumes as a microtask with the value. When it rejects, resume is a throw at the await line, which a local try/catch can handle.
You may await only inside async functions (and at the top level of a module). await in a non-async function is a syntax error. Top-level await delays the module's evaluation until the promise settles; modules that import it wait too. Use it for config load, not for a never-settling promise.
for...of plus await is sequential. Promise.all plus map is concurrent. That choice is the main performance fork. Sequential is simpler and rate-limit friendly. Concurrent is faster when the work is independent I/O.
for await...of walks an async iterable. It is the right loop for streams. On a plain array of promises it awaits each in order, which is sequential again.
An async function always returns a promise, even if you forget await inside. A missing await is a bug that returns too early and drops errors. return wait() and return await wait() differ in stack traces and in try/finally timing; return await runs finally after settlement.
What it costs you: a microtask per await, and the illusion of threads. The function looks synchronous. The thread is not blocked at await; other tasks run. Shared variables can change between two awaits. That is a race, just not a data race in the C++ sense.
Reach for async/await for sequential I/O and readable error handling. Reach for combinators when you need fan-out. Do not mark a function async only to use await 0 as a yield; use queueMicrotask or a timer if that is the intent.
Code Examples
async function loadUser(id) {
const res = await fakeFetch(id);
return res.email;
}
function fakeFetch(id) {
return Promise.resolve({ email: `user${id}@x.com` });
}
loadUser(7).then((email) => console.log(email)); // => user7@x.comasync function loadMany(ids) {
const emails = [];
for (const id of ids) {
emails.push(await loadUser(id)); // sequential
}
return emails;
}
async function loadManyFast(ids) {
return Promise.all(ids.map((id) => loadUser(id))); // concurrent
}
loadManyFast([1, 2]).then((xs) => console.log(xs));
// => [ 'user1@x.com', 'user2@x.com' ]async function failing() {
try {
await Promise.reject(new Error("nope"));
} catch (err) {
console.log(err.message); // => nope
}
}
failing();Common Gotchas
Forgetting `await` returns a promise and skips `try/catch`
the rejection becomes unhandled unless someone else awaits.
Sequential `await` in a loop can be much slower
use `Promise.all` when calls are independent.
Top-level `await` delays importers
a hung promise hangs the module graph.
`async` functions always return promises
`const x = async () => 1; x() === 1` is false.