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