0Pricing
JavaScript Academy · Lesson

Promises — States, Chaining, Error Handling

Create and use Promises: states (pending/fulfilled/rejected), chaining with then, and handling errors with catch/finally.

Promises — States, Chaining, Error Handling is a free JavaScript Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro to Promises

Goal: Learn tiny Promise patterns.

  • States: pending → fulfilled/rejected
  • then chaining
  • catch errors
  • finally cleanup
Promises — States, Chaining, Error Handling — illustration 1

Resolve a Promise

A Promise starts pending and becomes fulfilled when you call resolve.

// Create a Promise and resolve it
function waitMs(ms) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve("done");
    }, ms);
  });
}

console.log("Start");
waitMs(10).then(function (msg) {
  console.log("Resolved:", msg);
});
Promises — States, Chaining, Error Handling — illustration 2

Reject and catch

Use reject(Error) for failures. A catch handles any rejection above it.

// Reject to signal an error; handle with catch
function maybeFail(ok) {
  return new Promise(function (resolve, reject) {
    if (ok) {
      resolve("ok");
    } else {
      reject(new Error("bad"));
    }
  });
}

maybeFail(true)
  .then(function (v) {
    console.log("A:", v);
    return maybeFail(false);
  })
  .catch(function (e) {
    console.log("Caught:", e.message);
  });
Promises — States, Chaining, Error Handling — illustration 3

then chaining basics

Each then can return a value (or a Promise). The next step receives it.

// Chain then steps; return values flow to the next then
Promise.resolve(2)
  .then(function (n) {
    return n * 3;
  })
  .then(function (n) {
    console.log("Result:", n);
  });
Promises — States, Chaining, Error Handling — illustration 4

finally cleanup

finally is for cleanup after success or error; it does not receive the value/error.

// finally runs whether fulfilled or rejected
function task(ok) {
  return new Promise(function (resolve, reject) {
    ok ? resolve("yay") : reject(new Error("oops"));
  });
}

task(false)
  .catch(function (e) {
    console.log("Handled:", e.message);
  })
  .finally(function () {
    console.log("Cleanup done");
  });
Promises — States, Chaining, Error Handling — illustration 5

Promise.all taste

Promise.all resolves with an array of results or rejects if one fails.

// Promise.all waits for all; rejects fast if any fail
const a = waitMs(5);
const b = Promise.resolve("B");

Promise.all([a, b])
  .then(function (values) {
    console.log("All:", values);
  })
  .catch(function (e) {
    console.log("All failed:", e.message);
  });
Promises — States, Chaining, Error Handling — illustration 6

Promise error handling quiz

Quick check: Where to handle errors?

Promises — States, Chaining, Error Handling — illustration 7

Recap

Recap: Resolve for success, reject for errors, chain steps with then, handle problems with catch, and use finally for cleanup.

Promises — States, Chaining, Error Handling — illustration 8

Frequently asked questions

Is the “Promises — States, Chaining, Error Handling” lesson free?

Yes — the full text of “Promises — States, Chaining, Error Handling” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Promises — States, Chaining, Error Handling”?

Create and use Promises: states (pending/fulfilled/rejected), chaining with then, and handling errors with catch/finally. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Promises — States, Chaining, Error Handling” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Event Loop & Task vs Microtask Basics
  2. Promises — States, Chaining, Error Handling
  3. Promise.all / any / race / allSettled — choose the right tool
← Back to JavaScript Academy