Here is some a method that takes in an array of files (Files in HTML5 file API), and uses a promise to return the text in the file since reading is an async operation. The problem is that when I go to access the texts in a different method, it still contains promise objects that have been resolved, but I want to replace those promises with the actual text itself. The return statement gets called before the promise becomes resolved so I'm not entirely sure what to do.
function getTextFromJsonFiles(files) {
var texts = [];
var reader = new FileReader();
for (let file of files) {
var textPromise = new Promise(function (resolve, reject) {
reader.readAsText(file);
reader.onload = function (evt) {
resolve(evt.target.result);
};
});
texts.push(textPromise);
}
return texts;
}