Skip to content

Instantly share code, notes, and snippets.

@gil00pita
Forked from gisderdube/async_promise_all.js
Last active February 8, 2019 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gil00pita/d9a65fa830244224c4e30eff9b0c2526 to your computer and use it in GitHub Desktop.
Save gil00pita/d9a65fa830244224c4e30eff9b0c2526 to your computer and use it in GitHub Desktop.
https://levelup.gitconnected.com/9-tricks-for-kickass-javascript-developers-in-2019-eb01dd3def2a Promise.all What if you want to fetch all of the Pokemon in parallel? Since you can await all of Promises, simply use Promise.all :
import axios from 'axios'
let myData = [{id: 0}, {id: 1}, {id: 2}, {id: 3}]
async function fetchData(dataSet) {
const pokemonPromises = dataSet.map(entry => {
return axios.get(`https://ironhack-pokeapi.herokuapp.com/pokemon/${entry.id}`)
})
const results = await Promise.all(pokemonPromises)
results.forEach(result => {
updateData(result.data)
})
console.log(myData)
}
function updateData(newData) {
myData = myData.map(el => {
if(el.id === newData.id) return newData
return el
})
}
fetchData(myData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment