Glossary / Promises

Promises

A Promise is an object representing the eventual outcome of an operation: fulfillment with a value or rejection with a reason. Promises provide composable asynchronous control flow, but they do not make CPU work parallel and do not themselves cancel an operation. The Promise executor runs synchronously when the Promise is constructed; reactions registered through .then, .catch, and .finally run asynchronously as jobs.

Creating and consuming

const result = fetch("/api/item");
result.then((response) => response.json())
      .then((data) => render(data))
      .catch((error) => report(error));

A Promise can be pending, fulfilled, or rejected. “Resolved” is broader: a Promise may be locked to follow another Promise while still pending. Calling resolve with a thenable adopts its state; settling functions have no effect after the Promise is resolved. A Promise is settled once, but the operation that produced it may have side effects or continue after rejection/cancellation unless separately stopped.

Every .then returns a new Promise. Return values fulfill the next link; thrown values reject it; returning a Promise/thenable adopts its result. Ignoring a returned Promise can lose errors. Promise callbacks are microtasks in browsers, while hosts define integration details.

Use Promises for one eventual outcome. Use streams or async iterables for many values over time. Add explicit timeouts, cancellation signals, and concurrency controls when operations need them.

Interview questions

  1. Does Promise executor run asynchronously? No, it runs synchronously; reactions run later.
  2. Does a Promise cancel its underlying operation? No; cancellation must be coordinated separately.
  3. What does a handler return do? It determines the next Promise’s state/value.
  4. Promise versus stream? Promise is one eventual outcome; stream represents successive chunks.

References