COIN LIST 是一組加密貨幣([“BTCUSDT”,...])。我嘗試使用 getRSI 中的 getPriceAction 和 RSI 獲取價格,當我嘗試控制臺 DATA 時,這兩個函式正在作業。但是當我嘗試在回圈完成后列印回應時。它列印空陣列,該陣列的長度為 0。我想將 DATA 物件(由 SYMBOL、收盤價和 RSI 組成)存盤為回應陣列中的一個元素
import { COIN_LIST } from "./COIN_LIST.js";
import { getPriceAction } from "./PRICE_ACTION.js";
import { getRSI } from "./RSI.js";
async function main() {
try {
let response = await [];
await COIN_LIST.forEach((element, i) => {
setTimeout(() => {
let data = { symbol: element };
getPriceAction(element, "4h").then((res) => {
data.closingPrice = res;
getRSI(res).then((res) => {
data.RSI = res.reverse();
data.closingPrice = data.closingPrice.reverse();
response.push(data);
console.log(data)
});
});
}, i * 1000);
});
console.log(response);
} catch (error) {
console.log(error.message);
}
}
main();
uj5u.com熱心網友回復:
如果您想為您的代碼正確使用 async/await,請使用 async/await,也不要使用.then/.catch
一些顯著的變化
沒有增加秒數的 setTimeout ... 只需在一個結果后等待 1 秒,然后再獲得下一個結果 - 更清潔,如果一個請求恰好需要更長的時間,您不會一次收到兩個請求(這可能如果 API 受速率限制,則成為問題)
不.then... 使用async/await或.then/ .catch- 很少需要在一個函式中同時使用兩者
不要將 forEach 與async/一起使用await...它永遠不會做你想做的事,并且在 a 中創建一個 Promise 陣列.forEach非常幼稚,你不妨使用它.map!那么你可以await Promise.all(xxx.map(.....))- 但這對并發請求很有用,而不是像你的代碼那樣對串行請求有用
import { COIN_LIST } from "./COIN_LIST.js";
import { getPriceAction } from "./PRICE_ACTION.js";
import { getRSI } from "./RSI.js";
async function main() {
try {
const wait = (ms) => new Promise(resolve => setTimeout(resolve, 1000));
let response = []; //---> don't need that `await`
for (let element of COIN_LIST) {
let data = { symbol: element };
data.closingPrice = await getPriceAction(element, "4h");
const res = await getRSI(data.closingPrice);
data.RSI = res.reverse();
data.closingPrice = data.closingPrice.reverse();
response.push(data);
console.log(data);
await wait(1000);
}
console.log(response);
} catch (error) {
console.log(error.message);
}
}
main();
await wait(1000)可以根據 API 的速率限制進行調整......如果速率限制適用于發出請求時,您可以創建一個智能的函式來處理請求之間的延遲。
這種方式的代碼假設速率限制是基于上一個回應到下一個請求之間的時間段。
uj5u.com熱心網友回復:
回圈完成后,promise 還沒有得到解決,這就是它列印一個空陣列的原因。實作所需的一種方法是使用await for(...),或等待所有承諾得到解決,然后列印結果。
import { COIN_LIST } from "./COIN_LIST.js";
import { getPriceAction } from "./PRICE_ACTION.js";
import { getRSI } from "./RSI.js";
async function main() {
try {
let response = []; //---> don't need that `await`
const promises = []; //---> array of promises
COIN_LIST.forEach((element, i) => {
setTimeout(() => {
let data = { symbol: element };
const promise = getPriceAction(element, "4h").then((res) => {
data.closingPrice = res;
getRSI(res).then((res) => {
data.RSI = res.reverse();
data.closingPrice = data.closingPrice.reverse();
response.push(data);
console.log(data)
});
});
promises.push(promise) //---> save the reference to a promise
}, i * 1000);
});
await Promise.all(promises) //---> await for all promises to be resolved, then print the result
console.log(response);
} catch (error) {
console.log(error.message);
}
}
main();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462301.html
標籤:javascript api 异步 币安 加密货币
上一篇:具有多執行緒的異步服務器套接字
