В настоящее время вы ничего не return
делаете, просто записываете данные при вызове функции.
Вот пример:
function fetchYelp() {
let token = '<token>';
axios.get('https://api.yelp.com/v3/businesses/search?term=Church on the rock&location=saint peters, MO 63376',{
headers: {
Authorization: 'Bearer ' + token
}
})
.then(res => {
if (!res.data) {
// you should do something here so that you know there's no data, but returning here would exit out
return;
};
return res.data
})
.catch(err => {
console.log(err)
})
};
ESNext с асинхронным вызовом / ожиданием:
async fetchYelp() {
let token = '<token>';
try {
const res = await axios.get('https://api.yelp.com/v3/businesses/search?term=Church on the rock&location=saint peters, MO 63376',{
headers: {
Authorization: 'Bearer ' + token
}
})
if (!res.data) {
// again return something here, i'm just using an empty object as an example
return {};
}
return res.data
} catch (err) {
// throw can also be treated similar to a return statement
throw new Error(err)
}
}