0

I am trying to return the value of resp.data.stats.floor_price outside of the async function. Whenever I run the function and console.log the function value I get Promise {undefined}. When I console.log it inside of the function code it works. But when I try to return it and then console.log it outside, it doesn't work. How do I get the value of resp.data.stats.floor_price outside the async function. Here is the code:

const axios = require('axios').default;

async function getFloorPrice(collection,price){

    if (collection == '') {
        return console.log("Please provide a url collection name. This is the part after the https://opensea.io/collection/")
      }
      axios.get(`https://api.opensea.io/api/v1/collection/${collection}/stats`)
          .then(async resp => { 
        
              
            return await resp.data.stats.floor_price
            //if i console.log instead of return, it works
          })
      .catch(async err => {
              // Handle Error Here
              console.error(`The collection "${collection}" does not exist. Make sure to enter the collection url name properly. This is the part after the https://opensea.io/collection/`);
          });

}
const bob = getFloorPrice('boredapeyachtclub')
console.log(bob)
//bob returns Promise {undefined}
module.exports = getFloorPrice
YeetDev1
  • 1
  • 1
  • 1
    You forgot to `return` the axios Promise from the function – CertainPerformance Aug 19 '22 at 02:31
  • Note that even if you add the `return` in, `bob` will still be a promise; it will just be a promise which resolves to a useful value. `async` functions **always** return promises. To get access to the eventual value in the promise, you either need to call `.then` on it and put your code in the callback, or you need to `await` the promise. – Nicholas Tower Aug 19 '22 at 02:43

0 Answers0