← Back to JavaScript, page 1

javascript-0010 · Medium mid

How does async/await work, and how do you run async tasks in parallel with it?

An async function always returns a promise, and await pauses that function until a promise settles while the rest of the program keeps running. It is syntax over promises, so try/catch handles rejections. Awaiting tasks one after another makes them sequential, so start independent tasks first and await them together with Promise.all.

Full answer

Marking a function async does two things: it makes the function return a promise, and it allows the await keyword inside it. A returned value becomes the fulfilled value of that promise. A thrown error becomes a rejection.

await somePromise pauses only the async function. The engine stores its state, goes back to the event loop, and later resumes the function as a microtask once the promise settles. If it fulfills, await produces the value. If it rejects, await throws the reason at that line, so a normal try/catch/finally works.

The most common performance mistake is accidental serial work. Writing await a(); await b(); starts b only after a finishes. If the two tasks do not depend on each other, start both first and wait for both: await Promise.all([a(), b()]). Use Promise.allSettled when one failure must not hide the other results.

Awaiting inside forEach does not wait, because forEach ignores the returned promises. Use a for...of loop for sequential work or Promise.all with map for parallel work.

A function without await can still be async, but then it only wraps its result in a promise.

javascript
const wait = (ms, label) =>
  new Promise((resolve) => setTimeout(() => resolve(label), ms));

async function sequential() {
  const start = Date.now();
  const a = await wait(60, 'A');
  const b = await wait(60, 'B');
  return { results: [a, b], tookAtLeast120: Date.now() - start >= 115 };
}

async function parallel() {
  const start = Date.now();
  const [a, b] = await Promise.all([wait(60, 'A'), wait(60, 'B')]);
  return { results: [a, b], under100: Date.now() - start < 100 };
}

async function failing() {
  try {
    await Promise.reject(new Error('server down'));
  } catch (error) {
    return 'handled: ' + error.message;
  } finally {
    console.log('finally runs either way');
  }
}

(async () => {
  console.log(await sequential());
  console.log(await parallel());
  console.log(await failing());

  const order = [];
  [1, 2].forEach(async (n) => {
    await wait(10, n);
    order.push(n);
  });
  console.log('after forEach, order is', order);
  await wait(40);
  console.log('later, order is', order);
})();

Run with Node.js v24.22.1 as js-10.js; two runs gave identical output. Output: { results: [ 'A', 'B' ], tookAtLeast120: true } { results: [ 'A', 'B' ], under100: true } finally runs either way handled: server down after forEach, order is [] later, order is [ 1, 2 ]

Key points

  • async functions return a promise; await pauses only the current async function.
  • await turns a rejection into a thrown error, so try/catch works.
  • Sequential awaits run tasks one after another; Promise.all runs independent tasks together.
  • forEach does not wait for async callbacks.
  • Resumption after await happens as a microtask.

Common mistakes

  • Awaiting independent calls one by one and making the page slower than needed.
  • Using await inside forEach and expecting the loop to finish before the next line.
  • Forgetting that an async function without a try/catch around await passes the rejection to the caller.
  • Writing return await in a try block unnecessarily, or leaving it out when the catch must handle the error.

Follow-ups

  • What is the difference between Promise.all, Promise.allSettled, Promise.race, and Promise.any?
  • How do you add a timeout to an awaited call?
  • How would you limit concurrency when mapping over 1,000 items?

Tips

  • Always connect async/await back to promises and the microtask queue; it shows you know it is not magic.
  • Mention the serial-await mistake and its fix. It is the favourite follow-up.
  • Use for await...of when you need to mention async iterators.