I\'m trying to use FileReader to read several files sequentially using promises for that. The problem is that I need to divide the reading operations in several chunks to make t
Your upload()
function does not always return a Promise. It does in the else condition, but the if condition doesn't. You have a return statement, but it's inside of a callback, so it won't be received by the caller of upload
.
Try changing that to this:
function upload() {
if (start < end) {
return new Promise(function (resolve, reject) {
var chunk = uploading_file.slice(start, temp_end);
var reader = new FileReader();
reader.readAsArrayBuffer(chunk);
reader.onload = function(e) {
if (e.target.readyState == 2) {
content += new TextDecoder("utf-8").decode(e.target.result);
start = temp_end;
temp_end = start + BYTES_PER_CHUNK;
if (temp_end > end) temp_end = end;
resolve(upload());
}
}
});
} else {
uploading_file = null;
console.log(content); // it shows the content of the file
return Promise.resolve(content);
}
}
Now, upload
always returns a Promise instead of undefined
.