|
15 | 15 | | [Async/Await](#asyncawait) | |
16 | 16 | | [Async/Await & Error Handling](#asyncawait--error-handling) | |
17 | 17 | | [Async/Await vs Raw Promises](#asyncawait-vs-raw-promises) | |
| 18 | +| [Few Important Promise Methods](#few-important-promise-methods) | |
18 | 19 |
|
19 | 20 | ## Understanding Synchronous Code Execution ("Sync Code") |
20 | 21 |
|
@@ -920,3 +921,47 @@ However, if you try to use `await` outside of a function, it will result in a sy |
920 | 921 | ``` |
921 | 922 |
|
922 | 923 | So, to leverage the benefits of async/await, you need to structure your code within functions. |
| 924 | + |
| 925 | +## Few Important Promise Methods |
| 926 | + |
| 927 | +1. **[`Promise.race()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race)**: This method takes an array of promises and returns a new promise that resolves or rejects as soon as the first promise in the array resolves or rejects, whichever happens first. |
| 928 | + |
| 929 | + Useful when you want to perform multiple asynchronous operations and only need the result of the fastest one. |
| 930 | + |
| 931 | + ```javascript |
| 932 | + const promise1 = new Promise(resolve => setTimeout(resolve, 1000, 'one')); |
| 933 | + const promise2 = new Promise(resolve => setTimeout(resolve, 500, 'two')); |
| 934 | + |
| 935 | + Promise.race([promise1, promise2]) |
| 936 | + .then(result => console.log('Race Result:', result)) |
| 937 | + .catch(error => console.error('Race Error:', error)); |
| 938 | + // Output: Race Result: two (since promise2 resolves faster) |
| 939 | + ``` |
| 940 | + |
| 941 | +2. **[`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)**: This method takes an array of promises and returns a new promise that resolves when all promises in the array have resolved, or rejects if any promise in the array rejects. |
| 942 | + |
| 943 | + Useful when you want to wait for multiple asynchronous operations to complete before proceeding. |
| 944 | + |
| 945 | + ```javascript |
| 946 | + const promise1 = new Promise(resolve => setTimeout(resolve, 1000, 'one')); |
| 947 | + const promise2 = new Promise(resolve => setTimeout(resolve, 500, 'two')); |
| 948 | +
|
| 949 | + Promise.all([promise1, promise2]) |
| 950 | + .then(results => console.log('All Results:', results)) |
| 951 | + .catch(error => console.error('All Error:', error)); |
| 952 | + // Output: All Results: ['one', 'two'] (both promises resolve) |
| 953 | + ``` |
| 954 | + |
| 955 | +3. **[`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled)**: This method takes an array of promises and returns a new promise that resolves with an array of results, each corresponding to the input promises. The results contain information about whether each promise was fulfilled or rejected. |
| 956 | + |
| 957 | + Useful when you want to wait for all promises to settle (either resolve or reject) without stopping on rejection. |
| 958 | + |
| 959 | + ```javascript |
| 960 | + const promise1 = new Promise(resolve => setTimeout(resolve, 1000, 'one')); |
| 961 | + const promise2 = new Promise((resolve, reject) => setTimeout(reject, 500, 'error')); |
| 962 | +
|
| 963 | + Promise.allSettled([promise1, promise2]) |
| 964 | + .then(results => console.log('All Settled Results:', results)) |
| 965 | + .catch(error => console.error('All Settled Error:', error)); |
| 966 | + // Output: All Settled Results: [{ status: 'fulfilled', value: 'one' }, { status: 'rejected', reason: 'error' }] |
| 967 | + ``` |
0 commit comments