Util.mjs 792 B

12345678910111213141516171819202122
  1. /**
  2. * Like `Promise.allSettled()` but throws an error if any promises are rejected.
  3. */
  4. export const allSettledWithThrow = async (promises) => {
  5. const results = await Promise.allSettled(promises);
  6. const rejected = results.filter((result) => result.status === 'rejected');
  7. if (rejected.length) {
  8. for (const result of rejected) {
  9. console.error(result.reason);
  10. }
  11. throw new Error(`${rejected.length} promise(s) failed - see the above errors`);
  12. }
  13. // Note: TS was complaining about using `.filter().map()` here for some reason
  14. const values = [];
  15. for (const result of results) {
  16. if (result.status === 'fulfilled') {
  17. values.push(result.value);
  18. }
  19. }
  20. return values;
  21. };
  22. //# sourceMappingURL=Util.mjs.map