Promises in JavaScript

JavaScript·2 min read·Jan 1, 2025

In JavaScript, a promise is an object that represents the eventual completion or failure of an asynchronous operation and its resulting value.

Promises are generally used to handle long-running or parallel operations without blocking the Node.js process, especially when those operations involve waiting on I/O, such as reading and writing files, performing database queries, making HTTP calls to other services, and so on.

Promises can only be in one of three states:

  • Pending: The initial state, neither fulfilled nor rejected.
  • Fulfilled: The operation completed successfully.
  • Rejected: The operation failed.

Create promises

To create a new promise, you can instantiate the global built-in Promise class by passing an executor function to the constructor:

new Promise((resolve, reject) => {  if (success) {    resolve(value?);  } else {    reject(value?);  }});

Where:

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.