| 
 | 1 | +const promise1 = Promise.resolve(3);  | 
 | 2 | +const promise2 = new Promise((resolve) => setTimeout(() => resolve('foo'), 100));  | 
 | 3 | +const promise3 = Promise.reject('Error occurred');  | 
 | 4 | +const promise4 = Promise.resolve(42);  | 
 | 5 | + | 
 | 6 | +Promise.all([promise1, promise2, promise4])  | 
 | 7 | +    .then(values => {  | 
 | 8 | +        console.log(values); // [3, 'foo', 42]  | 
 | 9 | +    })  | 
 | 10 | +    .catch(error => {  | 
 | 11 | +        console.log('Caught:', error);  | 
 | 12 | +    });  | 
 | 13 | + | 
 | 14 | +Promise.all([promise1, promise2, promise3, promise4])  | 
 | 15 | +    .then(values => {  | 
 | 16 | +        console.log(values); // This won't execute  | 
 | 17 | +    })  | 
 | 18 | +    .catch(error => {  | 
 | 19 | +        console.log('Caught:', error); // Caught: Error occurred  | 
 | 20 | +    });  | 
 | 21 | + | 
 | 22 | +/**  | 
 | 23 | + * Explanation:  | 
 | 24 | + *   | 
 | 25 | + * This demonstrates Promise.all() behavior in JavaScript.  | 
 | 26 | + *   | 
 | 27 | + * 1. Promise.all() takes an array of promises and returns a single promise.  | 
 | 28 | + *   | 
 | 29 | + * 2. If ALL promises resolve, Promise.all() resolves with an array of all  | 
 | 30 | + *    resolved values, in the same order as the input promises.  | 
 | 31 | + *   | 
 | 32 | + * 3. If ANY promise rejects, Promise.all() immediately rejects with the  | 
 | 33 | + *    reason of the first promise that rejected. Other promises are ignored.  | 
 | 34 | + *   | 
 | 35 | + * 4. In the first example, all three promises resolve, so we get [3, 'foo', 42].  | 
 | 36 | + *   | 
 | 37 | + * 5. In the second example, promise3 rejects, so Promise.all() rejects  | 
 | 38 | + *    immediately with 'Error occurred', even though promise1 and promise4  | 
 | 39 | + *    would have resolved.  | 
 | 40 | + *   | 
 | 41 | + * 6. Promise.all() waits for all promises to settle (resolve or reject).  | 
 | 42 | + *   | 
 | 43 | + * Use Promise.allSettled() if you want to wait for all promises regardless  | 
 | 44 | + * of whether they resolve or reject.  | 
 | 45 | + */  | 
0 commit comments