为什么下面只显示我Promise { <pending> }
const Binance = require('binance-api-node').default;
const Client = Binance({
apiKey: binanceAPIKey,
apiSecret: binanceAPISecret
});
async function checkBTCAvgPriceLast24h() {
return await Client.dailyStats({symbol: "BTCEUR"});
}
console.log(checkBTCAvgPriceLast24h());
不应该await
等到解决或拒绝吗?
答案 0 :(得分:1)
因为 async
函数本身返回一个 Promise
,并且您的代码不会等待它解析。
尝试使用 .then()
链接您的函数调用,它应该可以工作。
checkBTCAvgPriceLast24h().then(res => console.log(res));
请参阅下面的代码片段以更好地理解:
const someAsyncTask = () =>
new Promise((res, rej) => {
setTimeout(() => {
res("Here's your data");
}, 1000);
});
async function main() {
return await someAsyncTask();
}
main().then((res) => console.log(res));