const promises = [Promise.resolve(1), Promise.resolve(2), Promise.reject(3)]; Promise.allSettled(promises).then(results => console.log(results)).catch(error => console.log('Promise.allSettled rejected with reason:', error));Bug: The bug is due to using Promise.all, which rejects as soon as one of the promises in the array rejects.
Explanation: The original code uses Promise.all, which rejects as soon as one promise in the array rejects. In contrast, Promise.allSettled waits for all promises to either resolve or reject and returns an array of objects describing the outcome of each promise. The fix involves replacing Promise.all with Promise.allSettled to get the desired output.
Key Insight: The difference between Promise.all and Promise.allSettled and when to use each.