Created
April 19, 2020 11:26
-
-
Save neuralline/9e46b85ef1fbe2adf83b746c7bde6782 to your computer and use it in GitHub Desktop.
Promise.allSettled async await loop with map
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
//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