promise {<pending>} - async function return await

Viewed 156

Why does the following only show me 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());

shouldn't await wait till resolved or rejected?

1 Answers

Because an async function itself returns a Promise, and your code is not waiting for it to resolve.

Try chaining your function call with .then() and it should work.

checkBTCAvgPriceLast24h().then(res => console.log(res));

See the code snippet below for better understanding:

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));

Related