简体   繁体   English

如何在每个请求仅支持一个 ID 的 API 端点中获取多个 ID?

[英]How do I fetch more than one ID in an API endpoint that only supports one ID per request?

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.我在图表中使用 Coingecko API 获取 output 的价格数据和时间(Unix),但我使用的端点仅支持每个请求一个 ID。

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我需要 function 也为 solana、cardano、ripple、dash 和 litecoin 做同样的事情

If your API only supports one ID per request, and you need multiple IDs, you're going to need to make multiple requests.如果您的 API 每个请求仅支持一个 ID,并且您需要多个 ID,那么您将需要发出多个请求。

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())
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM