0

I'm pretty new to programming, I was trying to built an API call that I will use in another function but I'm stuck here. I need to get a and b separately and as value out of this:

import axios from "axios";

async function main() {
  await axios
    .get('https://api.blocknative.com/gasprices/blockprices?confidenceLevels=99', {
      headers: {
        Authorization: 'key'
      },
    })
    .then(function(response) {
      const a = response.data.blockPrices[0].estimatedPrices[0].maxFeePerGas;
      const b = response.data.blockPrices[0].estimatedPrices[0].maxPriorityFeePerGas;
      return [a, b]
    });
};

main().catch(console.error);

How can I do it? I already take a look at scope,block and destructuring but I really can't figure a way out. Thanks for your help!

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 1
    You don't return anything from `main()` – phuzi Feb 28 '22 at 10:01
  • 1. `return` in front of `await axios` (you probably also don't need the `await` there). 2. [How to access the value of a promise?](https://stackoverflow.com/q/29516390) – VLAZ Feb 28 '22 at 10:04

1 Answers1

0

Let the main function return the promise and on resolve of that promise you will get a and b

import axios from "axios";

function main() {
  return await axios.get(
    "https://api.blocknative.com/gasprices/blockprices?confidenceLevels=99",
    {
      headers: { Authorization: "key" },
    }
  );
}
main()
  .then(function (response) {
    const a = response.data.blockPrices[0].estimatedPrices[0].maxFeePerGas;
    const b =
      response.data.blockPrices[0].estimatedPrices[0].maxPriorityFeePerGas;
    return [a, b];
  })
  .catch(console.error);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49