My lack of experience with async functions has me feeling a bit lost right now... I'm attempting to loop through files in a folder within a zip file using JSZip, store these files in an array, sort them, and then save them to a local variable for further use.
Below is my code where I extract an array of promises:
async extractTests(file){
let Zip = new JSZip();
let tests = await Zip.loadAsync(file).then((zip) => {
const promises = [];
zip.folder("tests").forEach(async function (relativePath, file) {
promises.push({ name: relativePath, data: await zip.file("tests/" + relativePath).async("text") });
});
return promises;
})
return tests;
}
Next, I attempt to sort the array in an event function triggered when a zip file is added:
extract(event) {
const file = event.target.files[0];
let res = this.extractTests(file);
res.then(function (r) {
res.sort(function (a, b) {
var nameA = a.name.toUpperCase();
var nameB = b.name.toUpperCase();
console.log(a.name);
if (nameA < nameB) {
return -1;
}
else {
return 1;
}
});
})
}
The reason for sorting the list is because it's currently getting resolved in the wrong order - I suspect the sort function isn't even firing. It seems like it may be related to an asynchronous function. Sorting would be easier on a local variable, so I'm looking for guidance on how to achieve that and resolve the promises.
Appreciate any help!