How can I push elements out of a forEach() context?
Congrats on your first post ever!
It's a super classic question though, how to use an asynchronous function in a loop. With .forEach
you can't, but you can using await
in a for
loop :
let promisesArray = [];
for (let object of ObjectJSON.array) {
if (object.key !== "foo") continue;
const response = await functionBar();
promisesArray.push(
`Object: ${object.key} | ${object.someOtherKey}`
);
}
resolve(promisesArray);
Using await
will probably require your function to be marked as async
(async function doSomething() { ...
)