I'm using JSZip to access files inside a zip. In that .zip there is one file that I'm looking for and I need it's content. To get the content it seems to require an async function. Which is called multiple times in a for loop.
My problem is that when I find the correct file, I'm in an async function so trying to get the value from the main program tell me 'undefined'. But I can't continue my code inside that async function because it's called in a loop so I would end running my main program many times.
var fileToFind;
JSZip.forEach(function (relativePath, file){
var fileAsStr = JSZip.file(relativePath).async("string"); //this is the way to get file content in JSZip
const waitStr = async () => {
fileAsStr = await fileAsStr;
const anchorStr = "Some string which is in only one file";
if(fileAsStr.indexOf(anchorStr) != -1){
fileToFind = fileAsStr;
console.log(fileToFind); //works ok
}
waitStr();
});
... //lot of stuff
console.log(fileToFind); //undefined