async function run(teamKey) {
let { data } = await axios.get(URL);
const { rounds } = data;
let goals = 0;
rounds.forEach((matchday) => {
matchday.matches.forEach((match) => {
if (match.team1.key == teamKey) {
goals += match.score1;
} else if (match.team2.key == teamKey) {
goals += match.score2;
}
});
});
console.log("goals: ", goals); // I can see the goals in console log
return goals; // but what's being returned is a pending promise
}
console.log("run(): ", run("arsenal"));
From what I can gather, the execution of run() completes and a pending promise is returned before axios.get() is resolved. From what I know about promises, there is only one way to get the goals and that is to chain a .then() after run(). Is there a way to get the run() function to return goals that can be used later in the code without making use of a chained .then()?
I tried everything, made another async function that called run() and returned the return value of run() but no luck.