Last active
March 11, 2019 00:27
-
-
Save jack-guy/14e05b9d51ba3b7fc8f8f0306765fca8 to your computer and use it in GitHub Desktop.
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
function getIceCreamById (iceCreamId) { | |
return fetch(`/icecream/${id}`); | |
} | |
const iceCreamIds = [4, 5, 10, 12]; | |
// Slow, effectively synchronous | |
for (let iceCreamId of iceCreamIds) { | |
console.log(await getIceCreamById(iceCreamId)); | |
} | |
// Concurrent, but waits for everything to complete before logging | |
const iceCreamsPromise = Promise.all(iceCreamIds.map(iceCreamId => getIceCreamById(iceCreamId))); | |
for (let iceCream of await iceCreamsPromise) { | |
console.log(iceCream); | |
} | |
// Concurrent, logs each as the request finishes | |
const iceCreamsPromise = await Promise.all( | |
iceCreamIds.map(async iceCreamId => { console.log(await getIceCreamById(iceCreamId)) }) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment