I have the following code where in the getStuff
function I'm making an external async call and storing the result in result
. From there I'm looping over result.Content.Ids
and pushing the Ids in myArray
.
When getStuff()
gets called in run()
, further down the code I need to access myStuff
array but if I console.log it out, it shows up as empty.
const myArray = [];
const getStuff = async (val) => {
const other = {}
if(val) other.newestVal = val;
const result = await someCall(other);
result.Content.Ids.forEach((id) => myArray.push(id));
if (result.AFieldExists) {
getStuff(result.newVal)
}
}
const run = async () => {
await getStuff();
// other code below that uses myArray
// but its empty when I log it
}
run();
I'd have expected myArray
to be populated since I'm awaiting getStuff()
in run()
. What am I doing wrong here?