Concept
Async/await
Writing asynchronous code that reads top to bottom, built on promises.
Async/await explained
async/await is syntax for working with promises that reads like ordinary top-to-bottom code. An async function always returns a promise. Inside it, await pauses only that function until the awaited promise settles, then resumes with its value, or throws its error so you can use a normal try/catch. The rest of the program keeps running in the meantime.
How async/await works
async function loadDashboard(userId) {
try {
const user = await getUser(userId);
// These two don't depend on each other: start both, then wait once.
const [orders, messages] = await Promise.all([
getOrders(user.id),
getMessages(user.id),
]);
return { user, orders, messages };
} catch (err) {
reportError(err);
throw err;
}
}
Each await hands control back to the event loop; the code after it runs later as a microtask. That is why awaiting things one after another is slower than starting them together when they are independent.
Common mistakes
- Accidental sequential work.
await a(); await b();takes the sum of both times. If they are independent, usePromise.all([a(), b()]). awaitinsideforEach.forEachignores the returned promises, so the loop finishes before the work does. Usefor...offor one-at-a-time, orPromise.all(items.map(...))for parallel.- Forgetting
await. You get a pending promise instead of the value, and errors escape yourtry/catch. - Swallowing errors with an empty
catch. Log or rethrow so callers know it failed.
async/await compared with promises and callbacks
Callbacks nest and spread error handling across every level. Promise chains flatten that and centralise errors in .catch. async/await is the same promises underneath, written so the happy path, loops and error handling look like synchronous code.
What interviewers ask about async/await
- "What does an
asyncfunction return?" Always a promise, even if you return a plain value. - "Does
awaitblock the thread?" No, only the current async function; the event loop keeps running other work. - "Rewrite this promise chain with async/await", then the reverse.
- "Run 100 requests, at most 5 at a time." Keep a pool of in-flight promises and start a new one each time one finishes.
What should you learn before Async/await?
- FunctionsReusable blocks of code; in JavaScript they are values that can be passed around and returned.
- CallbacksFunctions passed to other functions to be called later, the original way to handle async work.
- Promisesasync/await is syntax on top of promises.
- The event loopKnowing the microtask queue explains when await resumes.
Connections to other concepts
- Related
- Error handling
Practise Async/await
- Intermediate Node.js Interview Questions4 questions on Async/await
- Intermediate JavaScript Interview Questions3 questions on Async/await
- Node.js Interview Questions for Freshers2 questions on Async/await
- Top 20 JavaScript interview questions with answers: core JS, Node.js and React1 question on Async/await
- Advanced JavaScript Interview Questions1 question on Async/await