Skip to main content
⚑ OpenDevDocs
JavaScript
programmingjavascriptGuideIntermediate

JavaScript Promises and Asynchronous State

Mastering JavaScript Promises: pending/fulfilled/rejected states, then/catch/finally chaining, Promise.all, Promise.allSettled, and Promise.race.

Verified: 2026-09-30
Tested on:node 22.x
Platforms:
webnode
#javascript#promises#async#concurrency#state

JavaScript Promises and Asynchronous State

A Promise is a proxy for a value that is not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason.


1. The Three Promise States

                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β”‚       Promise [Pending]        β”‚
                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                 β–Ό (resolve)                     β–Ό (reject)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Promise [Fulfilled]        β”‚   β”‚       Promise [Rejected]       β”‚
β”‚      .then(value => ...)        β”‚   β”‚      .catch(error => ...)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  1. pending: Initial state, neither fulfilled nor rejected.
  2. fulfilled: The operation completed successfully.
  3. rejected: The operation failed with an error reason.

Once settled (fulfilled or rejected), a promise's state is immutable and cannot transition again.


2. Creating and Chaining Promises

function fetchUserData(userId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (userId > 0) {
        resolve({ id: userId, name: "Alex" });
      } else {
        reject(new Error("Invalid user ID"));
      }
    }, 500);
  });
}

// Consuming with .then / .catch / .finally:
fetchUserData(1)
  .then(user => {
    console.log("User retrieved:", user);
    return user.id; // Returns a new Promise resolved to user.id
  })
  .then(id => {
    console.log("User ID is:", id);
  })
  .catch(err => {
    console.error("Fetch failed:", err.message);
  })
  .finally(() => {
    console.log("Cleanup: request finished.");
  });

3. Promise Concurrency Combinators

CombinatorBehaviorFails Fast?Best Used For
Promise.all([p1, p2])Resolves when ALL promises fulfill. Rejects if ANY single promise rejects.βœ… YesParallel required requests (e.g. user + permissions).
Promise.allSettled([p1, p2])Waits for ALL promises to settle (never rejects). Returns array of {status, value/reason} objects.❌ NoBatch operations where partial failures are acceptable (e.g. sending batch emails).
Promise.race([p1, p2])Settles as soon as the FIRST promise settles (fulfilled or rejected).βœ… YesImplementing network timeouts.
Promise.any([p1, p2])Resolves as soon as the FIRST promise fulfills. Rejects only if all reject.❌ NoRedundant mirror fetching.

On this page