← Back to Node.js, page 2

nodejs-0028 · Easy fresher

How do you handle errors with async/await, and how do Promise.all, Promise.allSettled and 'return await' differ?

A rejected promise inside an awaited expression throws, so try/catch catches it. Promise.all rejects as soon as any promise rejects and ignores the others' results. Promise.allSettled waits for all and reports each outcome. In a try block, 'return somePromise' skips the catch, while 'return await somePromise' keeps it.

Full answer

Inside an async function, await turns a rejected promise into a thrown exception at that line. Wrap the awaited call in try/catch, or let the error propagate: an async function returns a promise that rejects with the error, and the caller handles it.

Running several tasks:

  • Promise.all([...]): resolves with all results in order. If any promise rejects, the result rejects with that first error. The other promises keep running, but their results and later errors are ignored.
  • Promise.allSettled([...]): never rejects. It resolves with an array of { status: 'fulfilled', value } or { status: 'rejected', reason }. Use it for batch jobs where each item may fail by itself, such as sending 100 emails.
  • Promise.race: settles with the first outcome. Promise.any: resolves with the first success and rejects with an AggregateError only if all fail.

A trap with return: inside try, return promise does not wait. The function leaves the try block at once, and a later rejection goes to the caller, not to your catch. Write return await promise when you want the local catch to run (or when a finally block must run after completion).

Another trap: an async function with a missing await. The promise is not handled, and a rejection becomes an unhandledRejection event, which ends the process in current Node.

javascript
const wait = (ms, value, fail) =>
  new Promise((resolve, reject) => setTimeout(() => (fail ? reject(new Error(value)) : resolve(value)), ms));

async function main() {
  // 1. Promise.all fails fast.
  try {
    await Promise.all([wait(10, 'maths'), wait(20, 'physics failed', true), wait(30, 'chemistry')]);
  } catch (err) {
    console.log('Promise.all rejected with:', err.message);
  }

  // 2. Promise.allSettled keeps every result.
  const results = await Promise.allSettled([wait(10, 'maths'), wait(20, 'physics failed', true), wait(30, 'chemistry')]);
  console.log(results.map((r) => (r.status === 'fulfilled' ? 'ok: ' + r.value : 'failed: ' + r.reason.message)));

  // 3. return vs return await inside try.
  async function withoutAwait() {
    try { return wait(5, 'without await: inner failure', true); }
    catch { return 'caught inside'; }
  }
  async function withAwait() {
    try { return await wait(5, 'with await: inner failure', true); }
    catch { return 'caught inside'; }
  }
  console.log('return promise      ->', await withoutAwait().catch((e) => 'caller caught: ' + e.message));
  console.log('return await promise ->', await withAwait());
}

main();

Without await, the rejection skips the local catch and reaches the caller. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): Promise.all rejected with: physics failed [ 'ok: maths', 'failed: physics failed', 'ok: chemistry' ] return promise -> caller caught: without await: inner failure return await promise -> caught inside

Key points

  • await converts rejections into exceptions that try/catch can catch.
  • Promise.all fails fast. Promise.allSettled reports every outcome.
  • return await keeps the local catch active.
  • A forgotten await creates an unhandled rejection.

Common mistakes

  • Using await in a loop for independent tasks and making them run one after another.
  • Using Promise.all for a batch where one failure must not hide the others.
  • Writing try { return promise } catch and expecting the catch to run.
  • Putting an async function in forEach and expecting it to be awaited.

Follow-ups

  • How would you run 1,000 tasks with at most 10 at a time?
  • What does Promise.any return when all promises fail?
  • What happens to the promises that are still running after Promise.all rejects?

Tips

  • Mention fail fast vs collect all in one sentence.
  • Use the return await trap to show depth.
  • Say that for independent work you start all promises first, then await them.