async function
An async function is a function that always returns a promise and can pause at each await expression until the awaited promise is fulfilled or rejected, without blocking the rest of the program.
The async keyword turns a function declaration, function expression, method, or arrow function into an async function, and await marks the points where it pauses. Each call creates a new Promise. A return resolves that promise with the returned value, and an uncaught exception rejects it instead of reaching the caller as a synchronous error.
The body runs synchronously until its first await. At that point, the caller gets back the pending promise, and the rest of the body resumes later as a microtask once the awaited promise settles. A rejected promise throws at the await that waits for it, so try and catch handle asynchronous failures the same way they handle synchronous ones.
Each await waits for the previous one to finish. Independent operations awaited one after another therefore run in sequence, unless the function starts them all first and then awaits them together.
Example
Say a checkout page looks up a discount for a coupon code and rejects unknown codes. The async function returns a plain number or throws, and the caller sees both outcomes through the returned promise:
> async function getDiscount(code) {
… if (code !== "SAVE10") {
… throw new Error(`Unknown coupon: ${code}`);
… }
… return 0.1;
… }
undefined
> getDiscount("SAVE10")
Promise { 0.1 }
> await getDiscount("SAVE10")
0.1
> await getDiscount("FREE").catch((error) => error.message)
'Unknown coupon: FREE'
The call returns a promise that wraps 0.1, and await unwraps it to the number. The body has no await, so it ran to the end during the call and the promise is already fulfilled. The thrown Error rejected the returned promise instead of escaping the call, and .catch() received it.
Say a dashboard needs a user name and an order count, and each lookup takes about 100 milliseconds. This program loads both with two awaits in a row, then starts both lookups first and awaits them together:
load-dashboard.mjs
const wait = (ms, value) =>
new Promise((resolve) => setTimeout(() => resolve(value), ms));
async function loadInOrder() {
const user = await wait(100, "Ada");
const orders = await wait(100, 3);
return { user, orders };
}
async function loadTogether() {
const [user, orders] = await Promise.all([wait(100, "Ada"), wait(100, 3)]);
return { user, orders };
}
let start = Date.now();
await loadInOrder();
console.log(`In order took 200 ms or more: ${Date.now() - start >= 200}`);
start = Date.now();
await loadTogether();
console.log(`Together took under 200 ms: ${Date.now() - start < 200}`);
Running the module times both versions:
$ node load-dashboard.mjs
In order took 200 ms or more: true
Together took under 200 ms: true
In loadInOrder(), the second lookup doesn’t start until the first await resumes, so the waits add up. In loadTogether(), both timers start before the function pauses, so the two waits overlap.
Drag the timeline or press Play to watch where each function pauses and when each lookup starts and settles:
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Nov. 16, 2026