#3 - Promises
Javascript promise is an object that acts as a proxy for an asynchronous function that is supposed to complete at some point. Promise supports the non-blocking asynchronous operations in javascript. So, with promises, the code doesn’t block, and the promise eventually returns a value.
A promise may exist in any of the three states.
- Pending: Initial state; neither fulfilled nor rejected
- Fulfilled: When the operation completes successfully
- Rejected: When the operation fails
Source
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise\
xxxxxxxxxx
11
const IPromise = new Promise((resolve,reject)=>{
setTimeout(()=>{
resolve("Hello");
},300);
});
//Output : Hello World
IPromise
.then(value=>{return value + " World"})
.then(value=>{console.log(value)})
.catch(err => {console.log(err)});
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment