1/*
2 A Promise is a proxy for a value not necessarily known when the promise is created.
3 It allows you to associate handlers with an asynchronous action's eventual success
4 value or failure reason.
5*/
6let promise = new Promise((resolve , reject) => {
7 fetch("https://myAPI")
8 .then((res) => {
9 // successfully got data
10 resolve(res);
11 })
12 .catch((err) => {
13 // an error occured
14 reject(err);
15 });
16});
1const myPromise = new Promise((resolve, reject) => {
2 setTimeout(() => {
3 resolve('foo');
4 }, 300);
5});
6
7myPromise
8 .then(handleResolvedA, handleRejectedA)
9 .then(handleResolvedB, handleRejectedB)
10 .then(handleResolvedC, handleRejectedC);
11
1new Promise((resolve, reject) => {
2 if (ok) { resolve(result) }
3 else { reject(error) }
4})
5
6
1myPromise
2.then(handleResolvedA)
3.then(handleResolvedB)
4.then(handleResolvedC)
5.catch(handleRejectedAny)
6.finally(handleComplition)
7