Module 24: Asynchronous Programming

Lesson 5 of 5 · 15–25 minutes

Handling errors in async code

🏠The everyday picture

A delivery app that shows a spinner forever when the restaurant closes is worse than one that says "Sorry, this restaurant isn't taking orders. Try another." Failure is normal; silence isn't acceptable.

📘What it really is

async function showProducts() {
  try {
    const products = await loadProducts();
    console.log(products);
  } catch (error) {
    console.log("Couldn't load products. Please try again.");
    console.error(error);
  } finally {
    console.log("Done loading.");
  }
}

🖱️Try it

Write unreliableLoad() that waits half a second, then fails half the time:

if (Math.random() < 0.5) throw new Error("Server busy");

Call it inside try/catch with a friendly message. Then write withRetry(task, attempts) that tries up to attempts times, waiting a second between tries, and gives up with a clear message.

✅Quick check

Where do you put code that should run whether loading succeeded or failed, like hiding a spinner?

Show the answer

In finally.

Why not retry a failed request forever?

Show the answer

If the problem isn't temporary, it never ends, and repeated requests can overload the server further.

← Previousasync and await
ProLearners Assist