Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nsulistiyawan/639318300b3faefd4b563f236d93683f to your computer and use it in GitHub Desktop.
Save nsulistiyawan/639318300b3faefd4b563f236d93683f to your computer and use it in GitHub Desktop.
function handleResults(results) {
const errors = results
.filter(result => result.status === 'rejected')
.map(result => result.reason)
if (errors.length) {
throw new AggregateError(errors)
}
return results.map(result => result.value)
}
async function loadInitialData() {
const results = await Promise.allSettled([
fetchUser(), fetchProduct(), fetchOther()
]);
try {
const [user, product, other] = handleResults(results)
} catch (err) {
for (const error of err.errors) {
handleTheError(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment