|
14 | 14 | // - Pending: Initial state, neither fulfilled nor rejected.
|
15 | 15 | // - Fulfilled: The operation completed successfully.
|
16 | 16 | // - Rejected: The operation failed.
|
17 |
| -// - You create a Promise using the Promise constructor, which takes an executor function as an argument. The executor function receives two functions: resolve and reject. You call resolve when the operation is successful, passing the result as an argument. You call reject when the operation fails, passing the error as an argument. |
| 17 | +// - You create a Promise using the Promise constructor, which takes an executor function as an argument. |
| 18 | +// - The executor function receives two functions: resolve and reject. |
| 19 | +// - You call resolve when the operation is successful, passing the result as an argument.You call reject when the operation fails, passing the error as an argument. |
18 | 20 |
|
19 | 21 | // Syntax
|
20 |
| -// const promise = new Promise((resolve, reject) => { |
21 |
| -// // Asynchronous operation here |
22 |
| -// if (/* condition for success */) { |
23 |
| -// resolve(value); |
24 |
| -// } else { |
25 |
| -// reject(error); |
26 |
| -// } |
27 |
| -// }); |
28 | 22 |
|
29 |
| -// Example: 1 |
| 23 | +// Basic Promise Creation |
| 24 | +const myPromise = new Promise((resolve, reject) => { |
| 25 | + // Asynchronous operation |
| 26 | + // If successful: |
| 27 | + resolve(result); |
| 28 | + // If unsuccessful: |
| 29 | + reject(error); |
| 30 | +}); |
| 31 | + |
| 32 | +// Chaining Promise |
| 33 | +myPromise |
| 34 | + .then((result) => { |
| 35 | + // Handle successful result |
| 36 | + return anotherPromise; // Return another Promise for chaining |
| 37 | + }) |
| 38 | + .then((result2) => { |
| 39 | + // Handle result from the second Promise |
| 40 | + }) |
| 41 | + .catch((error) => { |
| 42 | + // Handle errors from any of the Promises |
| 43 | + }); |
| 44 | + |
| 45 | +// Error Handling |
| 46 | +myPromise |
| 47 | + .then((result) => { |
| 48 | + // Handle successful result |
| 49 | + }) |
| 50 | + .catch((error) => { |
| 51 | + // Handle errors |
| 52 | + }); |
| 53 | + |
| 54 | +// Promise.all() |
| 55 | +Promise.all([promise1, promise2, promise3]) |
| 56 | + .then((results) => { |
| 57 | + // Handle successful results |
| 58 | + }) |
| 59 | + .catch((error) => { |
| 60 | + // Handle errors from all Promises |
| 61 | + }); |
| 62 | + |
| 63 | +// Promise.race() |
| 64 | +Promise.race([promise1, promise2, promise3]) |
| 65 | + .then((result) => { |
| 66 | + // Handle the result from the first Promise that resolves |
| 67 | + }) |
| 68 | + .catch((error) => { |
| 69 | + // Handle errors |
| 70 | + }); |
| 71 | + |
| 72 | +// Example |
30 | 73 | const promise1 = new Promise(function (resolve, reject) {
|
31 |
| - // Do async task |
32 |
| - // Database call, cryptography, network |
| 74 | + // Do async task like a database call, cryptography & network etc. |
33 | 75 |
|
34 | 76 | setTimeout(function () {
|
35 | 77 | console.log("Async task is complete");
|
|
0 commit comments