Overview Of Promises In JavaScript


Promise object helps you to run the functions asynchronously (sequentially) and use their return values when the processing is done.
 
Promises accept an executor function with the arguments resolve and reject methods. Once the task inside the Promise is completed, Promise can either be resolved or rejected.
 
States of Promise
 
A Promise passes through the states given below.
  • pending initial state of Promise when the instance gets created.
  • fulfilled the operation completed successfully.
  • rejected the operation failed.
Generally, Promises are created using a new keyword. Thus, when the instance gets created, then the stage is known as the pending stage and the task (or code) inside Promise block executes in this stage. On completion of the code execution, either Promise gets resolved or rejected.
 
then and catch
 
The callback methods, then and catch, are triggered when a Promise gets resolved or rejected respectively. When Promise gets resolved, Promise can send a value to the .then function. When Promise gets rejected, it can send an error message to the .catch function. In .then or .catch function, another Promise can be queued up.
 
Example
  1. var promise = new Promise(function(resolve, reject) {  
  2.     setTimeout(function() {  
  3.         resolve(“promise resolved”);  
  4.     }, 2000)  
  5. });  
  6. promise.then(function(msg) {  
  7.     console.log("then: " + msg); //then: promise resolved  
  8. }).catch(function(val) {  
  9.     console.log("catch: " + val)  
  10. });  
Promise.all
 
It accepts an array of Promises as a parameter. Promise.all gets fulfilled when all Promises in the array are resolved or gets rejected when any Promise gets rejected. If all Promises are resolved, .then callback method gets triggered with an array of the values in the same order of Promises added in the array parameter. If any Promise gets rejected, .catch method gets triggered with the error message returned by the rejected Promise.
 
Example
  1. var req1 = new Promise(function(resolve, reject) {  
  2.     setTimeout(function() {  
  3.         resolve({  
  4.             val: ‘Promise 1 resolved!’  
  5.         });  
  6.     }, 8000);  
  7. });  
  8. var req2 = new Promise(function(resolve, reject) {  
  9.     setTimeout(function() {  
  10.         resolve(‘Promise 2 resolved!’);  
  11.     }, 3000);  
  12. });  
  13. Promise.all([req1, req2]).then(function(response) {  
  14.     console.log('Then: ', response); // [Object, "Promise 2 resolved!"]  
  15. }).catch(function(err) {  
  16.     console.log('Catch: ', err);  
  17. });  
Promise.race
 
It also accepts an array of Promises as a parameter. Promise.race gets fulfilled or rejected as soon as any Promise gets resolved or rejected. Value of the .then or .catch depends on the value returned by the resolved or rejected Promise.
 
Example
  1. var req1 = new Promise(function(resolve, reject) {  
  2.     setTimeout(function() {  
  3.         resolve({  
  4.             val: ‘Promise 1 resolved!’  
  5.         });  
  6.     }, 8000);  
  7. });  
  8. var req2 = new Promise(function(resolve, reject) {  
  9.     setTimeout(function() {  
  10.         resolve(‘Promise 2 resolved!’);  
  11.     }, 3000);  
  12. });  
  13. Promise.race([req1, req2]).then(function(result) {  
  14.     console.log('Then: ', result); // then: Promise 2 resolved!  
  15. }).catch(function(err) {  
  16.     console.log('Catch: ', err);  
  17. });  
Available libraries for Promises
For further reading, I would refer
Thanks.