What is @ungap/promise-all-settled?
The @ungap/promise-all-settled npm package is a polyfill for the Promise.allSettled method, which is a part of the ECMAScript 2020 specification. This method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describe the outcome of each promise.
Polyfill for Promise.allSettled
This code demonstrates how to use the @ungap/promise-all-settled package to polyfill the Promise.allSettled method. It shows an array of promises, some of which resolve and one that rejects. Promise.allSettled is used to handle all the promises, and the results are logged to the console.
require('@ungap/promise-all-settled');
const promises = [
Promise.resolve(33),
new Promise(resolve => setTimeout(() => resolve(66), 0)),
99,
Promise.reject(new Error('an error'))
];
Promise.allSettled(promises).then(results => console.log(results));