I'm using Coingecko API for price data and time (Unix) to output this in a chart, but the endpoint I'm using only supports one ID per request.
document.onreadystatechange = async () => {
if (document.readyState === "complete") {
const coin = "bitcoin";
const currency = "brl";
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/${coin}/market_chart?vs_currency=${currency}&days=10&interval=hourly
`
);
const data = await response.json();
const prices = data.prices.map((e) => e[1]);
const date = data.prices.map(i => i[0]);
i need that the function do the same also for solana, cardano, ripple, dash and litecoin
If your API only supports one ID per request, and you need multiple IDs, you're going to need to make multiple requests.
One way to do them in parallel and wait for all to complete before continuing would be
const data = await Promise.all([
fetch(endpoint1).then(response => response.json()),
fetch(endpoint2).then(response => response.json()),
fetch(endpoint3).then(response => response.json())
]);
The above will result in data containing an array of the responses for each endpoint -- data[0] will be the json from endpoint1, data[1] from endpoint2, and so forth. You'll need to step through them to get your prices and dates for each of the IDs (obviously it wouldn't make sense for the Promise.all() to try merging all the responses into one, because you'd have no way of knowing which price went with which ID.)