Created
October 3, 2024 22:15
-
-
Save gtrabanco/974997fecfcad7b5a92fa3f36260da6d to your computer and use it in GitHub Desktop.
How Promise.allSettled works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}); |
Author
gtrabanco
commented
Oct 3, 2024

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