JavaScript: Understand Promise

Narayan Sharma
4 min readMay 9, 2019

The promise in JavaScript is very similar to the promise that you make in real life.

So, What will happen when you make a promise to someone close to assure about something?

  • The promise that you have made to someone is assurance/commitment that something will be done after specific time periods. So. that others can plan something based on your commitment.
  • The promise that you have made to someone is in a pending state until a specified time during the promise.
  • The promise you made can either be success or failure.
  • Once a promise is success others expect something out of that promise so that others can plan further based on output from the promise.
  • If the promise is a failure, once others know the reasons for the failure of promise so that they can plan what to do next.

The promises in JavaScript works exactly as the above points.

Definition of promise from MDN:
The promise in JavaScript objects that represent the eventual competition or failure of an asynchronous operation.

The states of the promise

Fulfilled: The promise will be considered as fulfilled when the asynchronous operation is completed. (Similarly, in real life, the moment when you fulfil the promise that you made to others)
Pending: The initial stage of promise. neither fulfilled or rejected. (Similarly, in real life from the moment you made promise to others to do something till the moment you broke or fulfilled your promise)
Rejected:
The promise will be called rejected when the given asynchronous operation is not completed. (Similar to the situation in real life, when you do not fulfil your promise in the given time)

Creating a Promise Object

We have created a promise object using promise constructor. It takes a single argument also known as an executor function having two callback functions resolve and reject.
Promise will be resolved by using resolve function and will be rejected by using the rejected function. Both functions accept only one argument.

You can also resolve and reject promise directly using promise.resolve() and promise.reject().

resolve and rejected methods defined below.

Uses of Promise

Promises are chainable, That means you can manipulate your result as many times as you want using then() function.

Exception Handle

This is very important while working with promise. If you do not handle exception properly your code gives you an unexpected response. To handle the exception for a promise you can use catch function.

Promise methods

  1. Promise.all(): This method is useful when you want to wait for more than one promise to complete and want to run all promise parallelly without blocking. If any promise in the array gets rejected the promise considered as a rejected promise.
simple promise

Let’s take an example if you are trying to get results from multiple endpoints you can use promise as like below.

2. Promise.resolve(): This method is useful when you want to wait for only one promise to complete. It will return promise object that is resolved with a given value.

3. Promise.reject(): It will return promise object that is rejected with the given reason. The rejected reason might be a network timeout or not getting a result as expected.

4. Promise.race(): It returns the promise that resolves or reject as soon as one of the promises in the iterable resolve or rejects with the value. Instead of waiting to resolve all the promise in iterable it waits for the one promise to be resolved or rejected.

Promise will be fulfilled because the first promise has no rejected.
It will be rejected because the first promise in the iterable object is rejected.

If you guys enjoyed by reading this article, Send claps as many as you can.
Thanks.

--

--