The JS Bifrost — All that we need to know about Promises
By: Raj Rawat
Tags: Best Practices, Promises, Programming, Javascript Development, JavaScript

A Promise is an object which is used to represent the eventual fulfilment or failure of an asynchronous task. It will produce a value as the result.

Little confusing isn’t it ??? Let’s understand in layman’s language.

Photo by bruce mars on Unsplash

We can say, a promise is some kind of agreement that will be fulfilled or denied if a certain condition is met. Also, instead of doing it right away, it will rather give value in near future.

Note: The IPL (Indian Premier League) is a professional Twenty20 cricket league in India.

Let’s understand promises with an IPL(I Promise ‘ll) Auction example

When a Franchise buys some player in an IPL auction let’s say Virat Kohli with the hope, that player will perform for them and win matches. We can say the player promises to a certain franchise he will score runs. After the tournament, if that player scores run in winning cause we can say that promise is fulfilled or resolved contrarily if he unable to score runs as expected we can say that promise is rejected or not fulfilled.

How does Promise Work?

When players came in IPL auction let’s say Pat Cummins with some start base price say 2 crores. Multiple franchises (KKR, CSK, MI, etc.) can place their bids for that player at the same time which should be higher than the base price let’s say 2.5 cr, 4.5 cr, 13cr, etc. This process of biding we can say it is the asynchronous task and multiple biding at the same time we can say multiple tasks are running in parallel which included KKR( promise), MI(callbacks), etc., After multiple bids, one franchise say KKR placed the highest bid say 13cr we can say KKR will get the player and Promise got resolved successfully.

How to create a Promise?

Now that we understand the concept of promise, let's deep dive and see how to implement it in JS.

A Promise can one have of the following states

Pending Fulfilled Rejected

Let’s understand these states with a cricketing example as IPL fever is at its peak.

Photo by Alessandro Bogliari on Unsplash

When any team takes review in cricket, it will be in the pending state till the time umpire is checking the necessary things(bats edge, etc.), After sometime when the umpire is fully sure about the decision, then he will decide and give it as out/not-out. The team who got the decision in their favor as out / not-out we can say that the reviewed decision as fulfilled if not as rejected or loss of review for the respective team.

There are methods provided by a promise to handle resolved/ rejected state.

Then Catch

When a promise gets resolved successfully it will call the .then() method where we can perform some operation like returning the resolve. On the other, if we get an error as a response then the .catch() method will get invoked.

In the above example, we are returning a promise object from fetchData method which is having the resolve and reject as callbacks. If the promise gets resolved successfully .then() method will be invoked or if it gets rejected or some error occurred then the .catch() method will be invoked.