I promise….
( 8 min reading and you will know what Javascript promises are )
The most commonly and famously asked question in the interview of a Javascript or frontend developer is: What are the promises? Why should we make promises? What are the states of promises? What are the promise chains? So what is a promise? Let’s try to understand it.
What is promise
As per the mdn web docs : The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
Bit technical definition to understand isn’t it?
Let’s try to understand promises with a story.
So suppose there is a family in which there are parents who have three children. One day, my parents were going to the supermarket. So one of their children asked them to bring ice cream for him or her. As usual, parents said okay to him or her.
Now that child asked the parents to please make me a promise that you will bring ice cream from the supermarket when you return, and the parent made a promise.
- Either parent will bring the ice cream from the supermarket and make their children happy.
- Or parents forgot to bring ice cream from the supermarket and made their children mad.
If parents bring ice cream for children, it means they have fulfilled their promise that they made to their child (this is a full-filled state of promise in JavaScript).
If parents didn’t bring the ice cream for children, it means they weren’t able to fulfill their promise (this is a rejected state of promise in JavaScript).
That’s it…. Yes, guys, this is actually what javascript promises are. Let’s understand now in technical terms.
Based on the technical definition promise is an object which eventually represents the completion or failure of an asynchronous operations.
Promises of having three states
1. Pending: It will be an initial state of promise.
2. Fulfilled: It will be a state when a promise is successfully completed (this will come under). then(), where we can pass or attach a successful function handler.
3. Rejected: It will be a state when a promise is rejected (this will come under). catch(), where we can pass or attach a rejected or failure function handler.
Why we should use the promise ?
Interesting another part of the promises is why we should use it? what kind of purposes it will resolve?
So, suppose if we are building a big application or if we are working on big projects where we need to make multiple asynchronous ajax calls and these ajax calls are dependent on each other, e.g., we need to make 2 ajax calls. makeAjax1(), makeAjax2(), and makeAjax2() calls should only be called if and only if the makeAjax1() call successfully returns.
In such cases, we can use promises.
Another bigger problem that we can solve using promises are callback hell scenarios( where the code or function has multiple callback functions and our code is structured like a pyramid of doom).
Another example is that we can get rid of giving our own code or function control to another function by passing the callback function.
In Such scenarios we can make our code more efficient and better if we will use promise.
Thanks for reading the article. Please let me know if I am able to clear a few doubts on the Javascript Promises.
In the next article, I will write about the promise chaining. Till that time, keep giving your support so that I can write more interesting topics in JavaScript.