Skip to content

Instantly share code, notes, and snippets.

@neuralline
Created April 19, 2020 11:26
Show Gist options
  • Save neuralline/9e46b85ef1fbe2adf83b746c7bde6782 to your computer and use it in GitHub Desktop.
Save neuralline/9e46b85ef1fbe2adf83b746c7bde6782 to your computer and use it in GitHub Desktop.
Promise.allSettled async await loop with map
//Promise.allSettled() vs Promise.all()
//allSettled: returns when all promises have either resolved or rejected
const getGitHubUser = async (usernames: []) => {
const result = await Promise.allSettled(
usernames.map(async (name: string) => {
try {
const response = await fetch(
`https://api.github.com/users/${name}`
)
const data = await response.json()
return data
} catch (err) {
console.error(`I'm down, this time. ${err}`)
}
})
)
return await result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment