JavaScript Promises

JavaScript Promises are a way to write asynchronous code in a more organized and easier to read way. They are a way to handle asynchronous operations without having to use callbacks.

Why should you use it?

  • Promises make it easier to write asynchronous code
  • They help to keep your code organized and readable
  • They are a great way to handle asynchronous operations

Promises

Promises are a way to handle asynchronous operations in JavaScript. They provide a way to react to the successful completion or failure of an asynchronous task. A promise is an object that represents the eventual completion or failure of an asynchronous operation. Promises are a way to write asynchronous code that is easier to read and debug. They are also a way to handle errors in asynchronous code without having to use try/catch blocks.

A promise can be in one of three states: pending, fulfilled, or rejected. The state of a promise can change from pending to fulfilled or rejected. When a promise is fulfilled, the value of the promise is set. When a promise is rejected, the reason for the rejection is set. A promise can only transition from pending to fulfilled or rejected once and then it is settled. Once a promise is settled, its state and value are immutable.

Creating a Promise

A promise can be created using the Promise constructor. The constructor takes a function as an argument. The function takes two arguments, resolve and reject. The resolve and reject arguments are functions that are used to set the state of the promise. The resolve function is used to set the state of the promise to fulfilled and the reject function is used to set the state of the promise to rejected.

script.js
const promise = new Promise((resolve, reject) => {
  // Do some async operation
  if (/* async operation is successful */) {
    resolve('Success!');
  } else {
    reject('Error!');
  }
});

Using a Promise

Once a promise is created, it can be used by calling the then method on the promise. The then method takes two arguments, a success callback and an error callback. The success callback is called when the promise is fulfilled and the error callback is called when the promise is rejected. The success callback is passed the value of the promise and the error callback is passed the reason for the rejection.

script.js
promise
  .then(
    (result) => console.log(result),
    (error) => console.log(error)
  );

Chaining Promises

Promises can be chained together using the then method. The then method returns a new promise that is resolved when the previous promise is resolved. This allows for asynchronous operations to be chained together in a way that is easier to read and debug.

script.js
promise
  .then((result) => {
    console.log(result);
    return new Promise((resolve, reject) => {
      // Do some more async operation
      if (/* async operation is successful */) {
        resolve('Success!');
      } else {
        reject('Error!');
      }
    });
  })
  .then(
    (result) => console.log(result),
    (error) => console.log(error)
  );