Skip to content

Instantly share code, notes, and snippets.

@neuralline
Created April 19, 2020 11:51
Show Gist options
  • Save neuralline/2d1ed30a25e33e3395f603b2946125e1 to your computer and use it in GitHub Desktop.
Save neuralline/2d1ed30a25e33e3395f603b2946125e1 to your computer and use it in GitHub Desktop.
async await for loop
//async for loop
//Why ? performance also browser support
const getGitHubUser = async (usernames: []) => {
//const results:[]:=[] //<-results array here
const length = usernames.length
for (let i = 0; i < length; i++) {
try {
const response = await fetch(
`https://api.github.com/users/${usernames[i]}`
)
const data = await response.json()
return data // results.push(data) //<-push to results array or call-back user(data)
} catch (err) {
console.error(`I'm down, this time. ${err}`)
}
}
return //await results
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment