Skip to content
Shubham JangleResources
JavaScript

Explore topics

What is Promise

Watch the walkthrough
Open on Instagram

Revision clarifications

The Promise constructor executor runs immediately; .then/.catch handlers run later as microtasks. Return a promise from a .then handler to wait for it. Resolve/reject settle a promise once, and a promise does not provide automatic cancellation.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

Promise

A Promise is a proxy for a value 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. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.

A Promise is in one of these states:

  1. pending: initial state, neither fulfilled nor rejected.
  2. fulfilled: meaning that the operation was completed successfully.
  3. rejected: meaning that the operation failed.

Example 1

const myPromise = new Promise((resolve, reject) => {
  // async task
  const status = 200;
  if (status === 200) {
    return resolve("๐Ÿ˜‰");
  } else {
    return reject("๐Ÿฅต");
  }
});

myPromise
  .then((result) => console.log(result))
  .catch((error) => console.log(error))
  .finally(() => console.log("Operation Completed"));

// Output
// ๐Ÿ˜‰
// Operation Completed

Example 2

function fetchTickets(projectId) {
  return new Promise((resolve, reject) => {
    console.log(`Fetching tickets: ${projectId}`);
    const tickets = ["T1", "T2", "T3"];
    resolve(tickets);
  });
}

function createDashboard(tickets) {
  return new Promise((resolve, reject) => {
    console.log(`Creating dashboard`);
    const dashboardId = "DASHBOARD-1";
    resolve(dashboardId);
  });
}

function sendNotification(dashboardId) {
  return new Promise((resolve, reject) => {
    console.log(`Notifying User: ${dashboardId}`);
    resolve();
  });
}

fetchTickets("PROJECT-1")
  .then((tickets) => createDashboard(tickets))
  .then((dashId) => sendNotification(dashId))
  .catch((error) => console.log(error))
  .finally(() => console.log("Done"));

// Output
// Fetching tickets: PROJECT-1
// Creating dashboard
// Notifying User: DASHBOARD-1
// Done