繁体   English   中英

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

[英]How do I fetch more than one ID in an API endpoint that 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]);

我需要 function 也为 solana、cardano、ripple、dash 和 litecoin 做同样的事情

如果您的 API 每个请求仅支持一个 ID,并且您需要多个 ID,那么您将需要发出多个请求。

并行执行它们并等待所有操作完成后再继续的一种方法是

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