I'm trying to learn async functions and am struggling to get the following to work.
async get_price(ticker) {
var price = await axios
.get(`https://marketwebsite.com/${ticker}`, {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36",
},
})
.then((res) => {
const $ = cheerio.load(res.data);
$(".todays_price").each((index, element) => {
var value = $(element)
.find(".current_price")
.text()
.replace(/\s\s+/g, "");
return value;
console.log(value); // THIS WORKS
});
})
.catch((err) => console.error(err));
}
When i call
var price = get_price(symbol);
I cannot get any return from this function. The console log noted above does work, however i cannot get the value to return from this function.
I have tried to await get_price(symbol) as well.
return priceinget_price();pricebecause you didn't writereturn price