Last active
August 5, 2021 19:53
-
-
Save gauthierm/d82671100fedc91eb1d36319e247fe2c 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
async function getAllMyData() { | |
const rootResponse = await fetch('https://www.myapi.com/foo', { ... }); | |
const rootData = await rootResponse.json(); | |
// this gets all the ids we need for the nested requests from the data | |
// in the first request | |
const ids = rootData.bars.map(bar => bar.id); | |
// fetch all the details of all the bar objects. This makes multiple | |
// requests that run in parallel and waits for all of them. The barData | |
// value will be an array of objects, one for each request. | |
const allBarData = await Promise.all(ids.map(async (id) => { | |
const response = await fetch('https://www.myapi.com/bar/' + id); | |
return await response.json(); | |
})); | |
// you could build a new returned object here that combines the | |
// allBarData with the rootData | |
return allBarData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment