Typorithm · medium

async/await Errors

async-error-handlinginterview-questionsasync-awaittry-catcherror-handling
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn async/await Errors

0%

Async & Event Loop

Awaiting a rejected promise throws, so wrap awaits in try/catch. Sequential awaits run one-after-another and are slow, while awaiting Promise.all runs them in parallel. An unhandled rejection propagates out of the async function as a rejected promise.

An await on a rejected promise throws at that line, exactly like a synchronous throw, so you guard it with a surrounding try/catch and the catch block receives the rejection reason.

Writing await a(); await b(); runs the calls sequentially — b only starts after a finishes — which is slow when they are independent; awaiting Promise.all([a(), b()]) instead kicks both off together and waits for both in parallel.

If you do not catch a rejection, it does not vanish: the async function itself returns a rejected promise, so the error propagates to that promise's caller and, if still unhandled, becomes an unhandledrejection.

01

await on a rejection throws — wrap awaits in try/catch.

02

Sequential awaits are slow; await Promise.all runs in parallel.

03

An uncaught rejection makes the async function return a rejected promise.

try {
  const data = await fetchUser(); // await rethrows a rejection
} catch (err) {
  console.error(err);             // caught here
}
const [a, b] = await Promise.all([taskA(), taskB()]); // parallel

Read the implementation

Connect each part of the code to the concept before you type.

1. Guard the await

Wrapping awaits in try/catch lets you handle a rejected promise as a thrown error.

try {

2. await throws

Awaiting a rejected promise throws synchronously at that point, so it lands in the surrounding catch.

const data = await fetchUser(); // await rethrows a rejection

3. Catch the error

The catch block receives the rejection reason exactly as if it were a thrown exception.

} catch (err) {

4. Handle it

Inside catch you can log, recover, or rethrow; the rejection is now a normal caught error.

console.error(err);             // caught here

5. Run in parallel

Awaiting Promise.all starts both tasks at once and waits together, unlike sequential awaits which run one after another.

const [a, b] = await Promise.all([taskA(), taskB()]); // parallel

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

async/await Errors: Lesson & Practice · Typorithm | ApplyDjinn