Promise: An Object represents an eventual completion or failure of an asynchronous operations and its resulting value.
Example:
Resolved promise.
const promise1 = new Promise(function(resolve, reject){
resolve('Water');
});
Rejected promise.
const promise2 = new Promise(function(resolve, reject){
reject(new Error("Disaster!"));
});
Promise takes an exector function as parameter which can have deciding factor to resolve or reject it.
-
Promise has 3 different states.
- pending: When the exection starts.
- fullfilled: When the promise gets resolved.
- rejected: When the promise gets rejected.
-
Promise can have 3 different values based on the state.
- undefined: When promise is in pending state, not fulfilled or rejected.
- value: When the promise gets resolved.
- error: When the promise gets rejected.
-
Promise provides 3 method handlers.
- .then()
- .catch()
- .finally()
Example:
const promise = new Promise(function(resolve, reject){
resolve('Water');
//reject(new Error("Jack fell down"));
});
const consumer = promise.then((result) =>
console.log(`Cooking with ${result}.`)
).catch((error) =>
console.log(`OMG ${error.message}.`));
consumer();
Output:
if promise gets resolved the output will be: Cooking with Water.
if promise gets rejected the output will be: OMG Jack fell down.