Skip to content

Instantly share code, notes, and snippets.

@jack-guy
Last active March 11, 2019 00:27
Show Gist options
  • Save jack-guy/14e05b9d51ba3b7fc8f8f0306765fca8 to your computer and use it in GitHub Desktop.
Save jack-guy/14e05b9d51ba3b7fc8f8f0306765fca8 to your computer and use it in GitHub Desktop.
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