Skip to content

Instantly share code, notes, and snippets.

@gtrabanco
Created October 3, 2024 22:15
Show Gist options
  • Save gtrabanco/974997fecfcad7b5a92fa3f36260da6d to your computer and use it in GitHub Desktop.
Save gtrabanco/974997fecfcad7b5a92fa3f36260da6d to your computer and use it in GitHub Desktop.
How Promise.allSettled works
class CustomError extends Error {
constructor(public data: any) {
super(JSON.stringify(data));
}
}
Promise.allSettled([
Promise.resolve(33),
new Promise((resolve) => setTimeout(() => resolve(66), 0)),
99,
Promise.reject(new Error('Message')),
Promise.reject(new CustomError({ foo: 'a data'})),
]).then((values) => {
console.log('3 is CustomError', values[3].reason instanceof CustomError);
console.log('4 is CustomError', values[4].reason instanceof CustomError);
console.log('4 is Error', values[4].reason instanceof Error);
throw new Error('Show allSettled Catch')
}).catch(errors => {
// Only show this if you throw an error in the Then
console.log('Catch')
console.error(errors)
});
@gtrabanco
Copy link
Author

Captura de pantalla 2024-10-04 a las 0 17 09

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment