I have a global variable called 'data' that is being modified inside a forEach loop. However, since the loop is asynchronous, the code does not wait until data is populated before it continues with the code. This is using the JSZip library.
let data = [];
await zip.folder("folderName").forEach(async function (relativePath, file) {
let json = await zip.file(file.name).async("text");
data.push(json);
console.log(data.length); // prints increasing numbers
});
console.log(data.length); //prints 0
// need to do something with data but it is empty
How do I wait for the data array to be populated before I continue with the code?
forEach
does not look to be async, so sending a promise to it wont work, its expecting a callback.. – Butanonemap()
which is not an option here, or manually iterating the set with afor
loop, which is also not possible here without making assumptions about the internals of the API. – Belorussia