A promise in JavaScript allows us to work with asynchronous work and make the code manageable for the JavaScript. It also returns a particular value based on it parameters. In easy words, a promise will make sure that under the hood while performing the asynchronous work of JavaScript (such as calling values from API) it will result in two actions, either it will be a success or it will be a failure.
It is very useful when we need to get result from a function which is quite time taking, then the promises will handle it by not returning the value immediately and making the page load slow rather it will make it work under the hood.
There are three states of promises-
- Pending
- Fulfilled
- Rejected

A pending state will eventually result in a fulfilled state or a rejected state.
Basic example
let displayResult = new Promise(function(resolve, reject){ //a call back function
a = 4;
b = 2;
if(b%a == 0){
resolve();
}else{
Reject();
}
});
displayResult.then(function (){
console.log(“success”);
}).catch(function(){
Console.log(“not a success”);
});
In the above code, the displayResult is declared with a promise with resolve and reject parameters which will return whether the function is resolved or rejected, based on the code inside it.
After this, we will fire .then to check the return value from the function of displayResult, which in case if resolved then will console.log / display success, and in case of there is some error the .catch will get the rejected value from the displayResult function and will console.log/ display not a success.
The very much high usability of promises can be seen in dependencies of asynchronous functions. Let’s say there are 3 functions that are going to return some values, and all are dependent on the precedent one.
let wakeUp = function(){
return new Promise ( function (resolve, reject){
resolve(‘woke at 8a.m’);
});
};
The above code is depicting that, if the promise is resolved then the user will get positive feedback with a display of woke at 8.am.
let busTime = function(){
return new Promise ( function (resolve, reject){
resolve(‘got bus at 9a.m’);
});
};
The above code is depicting that, if the promise is resolved then the user will get positive feedback with a display of getting the bus at 9 a.m.
let reachSchool = function(){
return new Promise ( function (resolve, reject){
resolve(‘reached school at 10a.m’);
});
};
The above code is depicting that, if the promise is resolved then the user will get positive feedback with a display of reached school at 10 a.m.
Using promises in dependencies
Now we will connect them all with the dependencies by calling the functions.
wakeup().then(function (){
return busTime();
})…
In the above code the wakeUp() function is called and only after the getting resolved value will proceed further as there is .then aspect, which will call the function busTime() inside the wakeUp(). Basically the above code acts as a promise on its own to call the busTime().
wakeup().then(function (){
return busTime();
}).then(function (){
return reachSchool();
})…
Similarly when first promise of wakeUp() with busTime() gets fired and receives some resolves value then only it will move further with .then to call a function named reachSchool().
wakeup().then(function (){
return busTime();
}).then(function (){
return reachSchool();
}).then(function (){
console.log(‘the function is finished’);
)};
At last when every previous promise gets executed with no rejection then it will land into the last promise which will give a confirmation that all the promises are now made as dependencies and works without any error and eventually will display the function is finished.
Using parameters to call values in Promises
As we can see that every time the promise gets resolved it is receiving a value or message. To access these values, we can simply pass the parameters in the promises of the main functions as well in the dependencies.
wakeup().then(function (result){
return busTime(result);
}).then(function (result){
return reachSchool(result);
}).then(function (result){
console.log(‘the function is finished’ + result);
)};
In the above code, we are calling the function with the result parameter. Which will receive as well as send value in result parameters. At last will console.log() the result too.
For getting the values from actual function just add the parameter of the message
let busTime = function(message){
return new Promise ( function (resolve, reject){
resolve(message + ‘got bus at 9a.m’);
});
};
let reachSchool = function(message){
return new Promise ( function (resolve, reject){
resolve(message +‘reached school at 10a.m’);
});
};
Once you get easy with the promises, try to implement this in the async and await functions in JavaScript. Refer to the article on What is AYNC AWAIT Function in JavaScript for better insights.


