I've been working on some new API projects and I'm a bit confused when it comes to callbacks. Essentially, I want to be able to return a value from within an asynchronous function. Every time I return the value, it is empty!
Please help!
const services = []
const nextechStatusRequest = function () {
// Web request to Nextech
axios.get(statusURI).then(response => {
// use Cheerio API to parse HTML Data
const $ = cheerio.load(response.data)
// Clean up
let data = $('table').text()
data = data.replace(/(\r\n|\n|\r)/gm, "")
data = data.split("•")
// Iterate through all services in string then create an Object to put into Services array
for (const item of data) {
if(item !== ''){
itemTrimmed = item.trim()
const service = {
serviceName: itemTrimmed.split(' (')[0],
serviceStatus: itemTrimmed.split('(')[1].replace(')', '').trim()
}
services.push(service)
}
}
})
return services
}
serv = nextechStatusRequest()
console.log(serv)