Promise in Javascript
A promise is a proxy object which used to handle asynchronous operations in JavaScript. It allows to specify a handler which associate with an asynchronous action's and can be either success value or failure. Rather, returning final value immediately, the asynchronous method returns a promise which supply the value at some point in the future.

Promise states

A promise can have either of the three states which can be either Resolved, Reject, Pending.
1. Pending : The Pending state is the initial state where the promise doesn't succeed or fail.2. Resolved : It specifies that the promise is executed successfully.3. Rejected : It is the last stage which specifies that the promise is failed to be executed.
Creating promise
 1 const promise = new Promise((resolve, reject) => {
 2   setTimeout(() => {
 3     resolve(1000);
 4   }, 300);
 5 });
 6 
 7 promise.then((value) => {
 8   console.log("Promise solved to :", value); // prints 1000
 9 });
 10 // it is pending once resolved, it executes then()
 11 console.log(promise); // Promise { <pending> }
In the above example, a Promise is define and by specifying a function that receives two function reference, resolve and reject. A setTimeout() function specified that wait for 300 milliseconds and resolved by specifying a value 1000 to resolve() function. Once a promise resolved, it executes success handler passed to then() method.
Output
 1 Promise { <pending> }
 2 Promise solved to : 1000

Promise with rejected

Promise rejected with message
 1 const promise = new Promise((resolve, reject) => {
 2   setTimeout(() => {
 3     reject("Invalid value !");
 4   }, 300);
 5 });
 6 
 7 promise.then(
 8     (value) => console.log("Promise solved to :", value),
 9     (rejected) => console.log(rejected) /* promise rejected error message */
 10 );
 11 // it is pending once resolved, it executes then()
 12 console.log(promise); // Promise { <pending> }
In the above example, a promise created by specifying a function that receives two methods reference resolve() and reject(). A reject() method called by passing a message that executed using setTimeout() after 300 milliseconds. A then() method called which specified with two handler success and rejected. As promise rejected, it execute rejected handler in then() method.
Output
 1 Promise { <pending> }
 2 Invalid value !

Constructor

A promise is a proxy object which is created by using the constructor method by specifying a function which does not returns an promise. The Promise() constructor is primarily used to wrap functions which does not support promises. It returns a promise object that can be resolved when either of the functions resolveFunc or rejectFunc.
Create promise using constructor
 1 new Promise(<executor>)

Concurrency methods

A Promise class provides many methods which can be used facilitate async task concurrency. It accepts array of promise object which executed concurrently and returns single promise object depends on the method used to perform operation.
1. all()2. allSettled()3. any()4. race()

Static methods

A Promise class provides many static method which can be used to create promise from the specified value or to facilitate async task concurrency. The static method resolve() and reject() static method used to create promise from the specified value or promise object.
1. resolve()2. reject()3. static concurrency methods

Instance methods

A Promise class provides three instance method to perform operation with an instance of the promise. It can be used to resolve, or rejection promise.
1. then()2. catch()3. finally()
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us