1 回答
TA贡献1785条经验 获得超8个赞
您也必须在您调用的函数中等待:
const getQuotes = async symbol => {
let quotes = {};
const client = await new DeltaRestClient(api_key, api_secret);
const linkOptions = createIdentifiers(symbol, false);
for (const entry of Object.entries(linkOptions)) {
const response = await client.apis.Products.getTicker({ symbol: entry[1] });
const ticker = JSON.parse(response.data.toString());
quotes[entry[0]] = parseFloat(ticker.result.close);
}
return quotes;
};
并相应地调用它:
const start = async () => {
const quotes = await getQuotes("ABCD");
console.log(quotes);
};
一般来说,混合 async/await 和 Promise.then/.catch 会导致容易被误解的可疑代码。
如果您愿意,您可以通过解构条目元素来提高可读性:
for (const [key, symbol] of Object.entries(linkOptions)) {
const response = await client.apis.Products.getTicker({ symbol, });
const ticker = JSON.parse(response.data.toString());
quotes[key] = parseFloat(ticker.result.close);
}
- 1 回答
- 0 关注
- 412 浏览
添加回答
举报