UPDATE: It seems that I misunderstood how promises work, leading to a synchronization issue. I mistakenly assumed that ".then" waits for the promise to resolve, which is not the case.
An unusual error has cropped up that has me stumped.
I'm puzzled by this code which generates the following results in the Chrome console.
Despite the array containing data and having a length, when I try to access the length property, it shows as zero. (I'm also unable to iterate over it using map)
Your insights and assistance in unraveling this mystery would be greatly appreciated.
const readFile = (file) => (
new Promise((resolve) => {
const fr = new FileReader();
fr.onload = () => {
resolve(fr.result);
};
fr.readAsText(file);
})
);
const readFiles = (files) => {
const readFiles = [];
files.forEach((file) => (readFile(file)).then((data) => readFiles.push({name: file.name, data })));
return readFiles;
};
const scenes = readFiles(...grabbed from file picker dialog...)
console.log('scenes: ', ui.value.scenes);
console.log('length: ', ui.value.scenes.length);