🌽Promises

In JavaScript, a promise is an object that represents the result of an asynchronous operation. A promise can be in one of three states:

  • Pending: The asynchronous operation has not completed yet.

  • Fulfilled: The asynchronous operation has completed successfully.

  • Rejected: The asynchronous operation has failed.

A promise is created using the Promise constructor, and is passed a function called the "executor" function. The executor function is responsible for starting the asynchronous operation, and it is called with two functions as arguments: resolve and reject.

The resolve function is called when the asynchronous operation succeeds, and the reject function is called when the operation fails.

Here is an example of a promise:

const promise = new Promise((resolve, reject) => {
  // Perform an asynchronous operation
  setTimeout(() => {
    // If the operation succeeds, call resolve
    resolve('Success');
  }, 1000);
});

promise.then(result => {
  // This function will be called when the promise is fulfilled
  console.log(result); // 'Success'
});

In this example, the promise is created with a setTimeout function as the asynchronous operation. The setTimeout function is set to complete after 1 second (1000 milliseconds), and when it does, it calls the resolve function with the value 'Success'.

The then method is used to specify a function to be called when the promise is fulfilled. In this case, the function logs the result of the asynchronous operation to the console.

Promises are often used to handle the results of asynchronous operations in a more organized and easier-to-read way. They provide a way to simplify the handling of asynchronous operations, and are a common pattern in JavaScript programming.

Here is an example of a promise using the then and catch methods:

const promise = new Promise((resolve, reject) => {
  // Perform an asynchronous operation
  setTimeout(() => {
    // If the operation succeeds, call resolve
    resolve('Success');
  }, 1000);
});

promise
  .then(result => {
    // This function will be called when the promise is fulfilled
    console.log(result); // 'Success'
  })
  .catch(error => {
    // This function will be called when the promise is rejected
    console.error(error);
  });

In this example, the then method is used to specify a function to be called when the promise is fulfilled, and the catch method is used to specify a function to be called when the promise is rejected.

The then function receives the result of the asynchronous operation as an argument, and the catch function receives the error that occurred.

If the asynchronous operation succeeds, the resolve function is called and the promise is fulfilled. The then function is called with the result of the operation, and the catch function is not called.

If the asynchronous operation fails, the reject function is called and the promise is rejected. The catch function is called with the error that occurred, and the then function is not called.

The then and catch methods allow you to handle the results of a promise in a more organized and easier-to-read way. They provide a way to simplify the handling of asynchronous operations, and are a common pattern in programming.

Last updated