Skip to content

Instantly share code, notes, and snippets.

@nsulistiyawan
Created February 19, 2023 08:55
Show Gist options
  • Save nsulistiyawan/0247c3ef7cf8624ca96a1ad948deece4 to your computer and use it in GitHub Desktop.
Save nsulistiyawan/0247c3ef7cf8624ca96a1ad948deece4 to your computer and use it in GitHub Desktop.
async function loadInitialData() {
// fire and await together
const [userResult, productResult, otherResult] =
await Promise.allSettled([
fetchUser(), fetchProduct(), fetchOther()
])
// process user
if (userResult.status === 'rejected') {
const err = userResult.reason
handleError(err)
} else {
const user = userResult.value
}
// process product
if (productResult.status === 'rejected') {
const err = productResult.reason
handleError(err)
} else {
const product = productResult.value
}
// process other
if (otherResult.status === 'rejected') {
const err = otherResult.reason
handleError(err)
} else {
const other = otherResult.value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment