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.

Lesson 2 of 38 steps

Promises — States, Chaining, Error Handling is a free JavaScript Academy lesson on CoddyKit. This is lesson 2 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

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
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

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

Yes — any 3 lessons of the JavaScript Academy learning path, including “Promises — States, Chaining, Error Handling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.

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