JavaScript Tutorial

Javascript Promises

Promise in JavaScript is like a placeholder for a future value or result of an asynchronous operation. It represents something that will eventually happen, such as fetching data from a server or loading an image.

In other words, Promise is an object that represents the eventual completion or failure of an asynchronous operation. It is used for managing and handling asynchronous operations.

A Promise can be in one of three states:

  1. Pending: The initial state when the Promise is created, and the asynchronous operation is still in progress.

  2. Fulfilled: The state when the asynchronous operation is successfully completed, and the Promise holds the resulting value.

  3. Rejected: The state when the asynchronous operation encounters an error or fails to complete, and the Promise holds the reason for the failure.

  4. Settled: Either the action is fulfilled or rejected.

Once a Promise is created, you can attach callbacks to it used .then() to handle the successful completion or .catch() to handle any errors. These callbacks will be triggered when the Promise is fulfilled or rejected, respectively.

Promises provide a more structured and readable way to handle asynchronous code compared to nested callbacks. They help in managing the flow of asynchronous operations, allowing you to write cleaner and more maintainable code.

Creating a Promise

To create a promise in JavaScript, we use the Promise constructor:-

Input:-

Output(before 3 seconds):-

Output(after 3 seconds):-

Now, we see that the promise starts with the pending state, when the value is undefined. The promised value will be returned once the promise is completed.

After 3 seconds, type the learn in the console window, we will see that the state of the promise becomes resolved and the promised value is the string that we passed to the resolve() function.

So calling the resolve() function moves the promise object to the fulfilled state.

If the promise reaches fulfilled state or rejected state, the promise is resolved.

So, a promise represents the completion of an asynchronous operation with its result. It can either signify the successful completion of the promise, or its failure, but eventually completed.

Promises of Promise

JavaScript Promise promises that:-

  • Unless the current execution of the js event loop is completed (success or failure), callbacks will never be called.
  • Even if the callbacks with then() are present, they will be called only after the execution of the asynchronous operations completely.
  • Multiple callbacks can be executed, by invoking then() many times, each of them will be executed in a chain, which means one after the other, following the sequence in which they were inserted.

 

Methods in Promise

The functions of Promise are executable almost on every trending web browser such as Mozilla, Chrome, Opera, etc. Following is the methods list:-

Method Name

Summary

Promise.resolve(promise)

Returns promise only if promise.constructor==Promise.

Promise.resolve(thenable)

Makes a new promise from thenable containing then().

Promise.resolve(obj)

Makes a promise resolved for an object.

Promise.reject(obj)

Makes a promise rejection for the object.

Promise.all(array)

Makes a promise resolved when each item in an array is fulfilled or rejects when items in the array are not fulfilled.

Promise.race(array)

If any item in the array is fulfilled as soon, it resolves the promise, or if any item is rejected as soon, it rejects the promise.

Go back to Previous Course