Skip to content

Instantly share code, notes, and snippets.

@onosendi
Created June 30, 2023 18:48
Show Gist options
  • Save onosendi/446099464d32c4c0ce4aa58ea7d972eb to your computer and use it in GitHub Desktop.
Save onosendi/446099464d32c4c0ce4aa58ea7d972eb to your computer and use it in GitHub Desktop.
const fn = (x) => new Promise((resolve, reject) => {
if (x === 0) {
reject(new Error('Error: equals 0'));
} else {
resolve(`Resolved: ${x}`);
}
});
const ps = [
fn(1),
fn(2),
fn(0),
fn(3),
];
Promise
.allSettled(ps)
.then((res) => {
const errors = res.flatMap((o) => (o.status === 'rejected' ? o.reason.message : []));
const ress = res.flatMap((o) => (o.status === 'fulfilled' ? o.value : []));
console.log(errors);
console.log(ress);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment