1var promise = new Promise(function(resolve, reject) {
2 // do some long running async thing…
3
4 if (/* everything turned out fine */) {
5 resolve("Stuff worked!");
6 }
7 else {
8 reject(Error("It broke"));
9 }
10});
11
12//usage
13promise.then(
14 function(result) { /* handle a successful result */ },
15 function(error) { /* handle an error */ }
16);
1const promise1 = new Promise((resolve, reject) => {
2 setTimeout(() => {
3 resolve('foo');
4 }, 300);
5});
6
7promise1.then((value) => {
8 console.log(value);
9 // expected output: "foo"
10});
11
12console.log(promise1);
13// expected output: [object Promise]
1Promises make async javascript easier as they are easy to use and write than callbacks. Basically , promise is just an object , that gives us either success of async opertion or failue of async operations
1myPromise
2.then(handleResolvedA)
3.then(handleResolvedB)
4.then(handleResolvedC)
5.catch(handleRejectedAny)
6.finally(handleComplition)
7