-
-
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 :
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
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