Skip to content

Instantly share code, notes, and snippets.

@dylannnn
Forked from gisderdube/async_promise_all.js
Created January 17, 2019 09:37
Show Gist options
  • Save dylannnn/bb609b8ce930a77dd081ee2f6917819a to your computer and use it in GitHub Desktop.
Save dylannnn/bb609b8ce930a77dd081ee2f6917819a to your computer and use it in GitHub Desktop.
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