Skip to content

Instantly share code, notes, and snippets.

@gauthierm
Last active August 5, 2021 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauthierm/d82671100fedc91eb1d36319e247fe2c to your computer and use it in GitHub Desktop.
Save gauthierm/d82671100fedc91eb1d36319e247fe2c to your computer and use it in GitHub Desktop.
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