我使用Coingecko对价格数据和时间(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]);我需要这个功能也同样适用于索拉娜,卡达诺,波纹,仪表板和里特科林。
发布于 2022-02-02 21:56:58
如果您的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())
]);上述结果将导致data包含每个端点的响应数组-- data[0]将是来自endpoint1的json,来自endpoint2的data[1],等等。您需要逐步了解它们,以获得每个ID的价格和日期(显然,Promise.all()尝试将所有响应合并为一个没有意义,因为您无法知道哪个ID的价格。)
https://stackoverflow.com/questions/70963278
复制相似问题