Unlisted
Edited
May 26, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// function for accessing data
async function getFacts({animal, amount}) {
let response = await fetch(
`https://cat-fact.herokuapp.com/facts/random?animal_type=${animal}&amount=${amount}`
);
return await response.json();
}
Insert cell
// make an array of api params
calls = [
{animal: "cat", amount: 5},
{animal: "dog", amount: 5},
{animal: "horse", amount: 5},
]
Insert cell
Insert cell
// map and flatten through array of parameters
(await Promise.all(calls.map(getFacts))).flat()
Insert cell
Insert cell
// function for accessing data
function getFactsTest(animal, amount) {
let baseUrl = `https://cat-fact.herokuapp.com/facts/random?animal_type=${animal}&amount=${amount}`
return d3.json(baseUrl)
}
Insert cell
// make an array of api params
callsTest = [
{animal: "cat", amount: 5},
{animal: "dog", amount: 5},
{animal: "horse", amount: 5},
]
Insert cell
Insert cell
// map through array of parameters
promisesTest = callsTest.map(d => getFactsTest(d.animal, d.amount))
Insert cell
// resolve promises
resultTest = Promise.all(promisesTest)
Insert cell
// flatten
resultTest.flat()
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more