Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 24 / 31TIME AND MODULES

A promise is a future result.

Promises describe eventual success or failure and compose dependent steps.

01 / THE BASICS

Start here.

An asynchronous operation may produce a result later. A Promise is an object representing that future outcome. It begins pending, then settles either fulfilled with a value or rejected with a reason. Settlement happens once. A promise is not the result itself: a function returning Promise<number> in a typed description is giving you a route to a number, not an immediate number.

then registers a function to run after fulfillment. catch registers a handler for rejection. Each then call returns a new promise, which lets one stage transform a value for the next stage. If a stage returns another promise, the chain waits for it. If a stage throws, the returned promise rejects. That is why an error handler placed later in the chain can receive failures from earlier stages.

A useful mental model is a sequence of future transformations. Start with the operation, identify what value it fulfills with, then trace what each then callback returns. A missing return can make the next stage receive undefined. A rejected promise needs a deliberate response: show a message, retry when appropriate, or pass the failure onward. The presence of a promise does not guarantee success; it describes how success and failure will be delivered.

02 / THE IDEA

Build the model.

A Promise starts pending, then settles as fulfilled or rejected. Handlers registered with then or catch run later as microtasks. A promise represents the eventual result of work; it does not itself guarantee that the underlying work can be cancelled.

Returning a value from then feeds the next step. Returning another promise makes the chain wait for it. Throwing inside a handler turns the next promise into a rejection. Always consider where a rejection will be handled, especially when a promise is started without await.

03 / UNDER THE SURFACE

How it works.

A promise represents a value that may arrive later or a failure that may be reported later. It begins pending and settles only once, either fulfilled or rejected. Calling then registers what to do with fulfillment; catch handles rejection. These handlers do not run synchronously at the registration line, even if the promise is already settled.

A then call produces a new promise. If its handler returns a plain value, the next promise fulfills with that value. If it returns another promise, the chain waits for it. If it throws, the next promise rejects. This is why one catch at the end can handle failures from several earlier steps. Always return the promise from a handler when the next step must wait for it.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
Promise.resolve({ theme: 'dark' })
  .then(settings => settings.theme)
  .then(theme => console.log(theme))
  .catch(error => console.error(error));

First notice. Each handler receives the value returned by the step before it.

Promise.resolve creates a promise fulfilled with the settings object. The first handler receives that object and returns its theme string, 'dark'. The next promise fulfills with that string, so the second handler displays dark. If either handler threw, the catch handler would receive the error. The chain describes value flow and failure flow together.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
Promise.resolve(3)
  .then(n => n * 2)
  .then(n => console.log(n));

The first handler receives 3 and returns 6. That fulfills the next promise with 6, so the second handler prints 6. The callbacks run in microtasks after the current synchronous turn. If the first handler threw, the second success handler would be skipped unless a catch recovered.

06 / KEEP THIS

One thing to remember.

A promise chain is a path for values and failures through time.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.