Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript5 tutorials
  • Node.js Introduction2 tutorials
  • JavascriptJavascript · Beginner · Mid
  • JavascriptJavascript · Beginner · Short
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 15

Promises: Values That Arrive Later

Some work on a web page takes time — downloading data from a server, for example. JavaScript does not stop and wait. Instead, it uses a **promise**, which is an object that acts as a placeholder for a value that will arrive later.
Level: BeginnerLength: Mid2 min read

Read at another depth

Promises: Values That Arrive Later

Some work on a web page takes time — downloading data from a server, for example. JavaScript does not stop and wait. Instead, it uses a promise, which is an object that acts as a placeholder for a value that will arrive later.

Think of it like a ticket at a restaurant. You order food and get a ticket. The food is not ready yet, but the ticket guarantees that something will come — or that something will go wrong. In JavaScript terms, a promise is either pending (still working), fulfilled (the value arrived), or rejected (something failed).

Creating and Using a Promise

You rarely create promises yourself at first — browser functions like fetch (which downloads data) return them. You get the value using the .then() method, which runs a function when the promise fulfills, and .catch(), which runs when it rejects.

Code example in javascript
const promise = fetch('https://api.example.com/data');

promise
  .then(response => {
    console.log('The data arrived!', response.status);
  })
  .catch(error => {
    console.log('Something went wrong:', error.message);
  });

Notice the code reads top to bottom, even though the value arrives later. The .then() part is like saying: "When the food is ready, do this."

Chaining: Work in Order

A promise's .then() returns a new promise, so you can chain steps that run one after another. This is how you do several things in sequence with slow operations.

Code example in javascript
function wait(milliseconds) {
  return new Promise(resolve => {
    setTimeout(() => resolve('done waiting'), milliseconds);
  });
}

wait(1000)
  .then(message => {
    console.log(message); // after 1 second: "done waiting"
    return wait(500);     // return another promise
  })
  .then(message => {
    console.log(message); // 0.5 seconds later
  });

Here, wait returns a promise. The second .then() only runs after the first promise fulfills — the chain keeps things in order.

Why Not Just Wait?

If JavaScript stopped and waited for each download, the page would freeze. Buttons would not respond, animations would stop. Promises let the browser keep running other work while the slow task finishes in the background.

Common Mistakes

  • Forgetting .catch(). If a promise rejects and nothing handles the error, the failure is silent and hard to debug.
  • Trying to use the value directly: console.log(fetch(url)) logs a promise object, not the data.
  • Forgetting to return inside a .then() when you want the next step to wait.

Exercise

What does calling a function like fetch() actually return, and how do you get the real data from it?

Show answer

It returns a promise — a placeholder for the future value. You get the real data by attaching .then() to the promise (or by using await), which runs once the promise fulfills.

Exercise

A promise can be pending, fulfilled, or rejected. What happens if it rejects and you never wrote a .catch()?

Show answer

The error goes unhandled. The code in .then() never runs, and the failure happens silently, which makes bugs hard to find. Always add a .catch().

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Promises: Values That Arrive Later

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that