从Javascript 发展而来,用 Promises 为大文件拼接 FileReader,怎么样?,它向我展示了 Promise 如何也可以解析函数,现在我被困在相同但在 Array.reduce 函数中。
目标是我想在数组中上传一个文件(已经上传),其中每个数组项(一个文件)按顺序上传(即通过承诺控制)。
然后,我知道答案在http://www.html5rocks.com/en/tutorials/es6/promises/?redirect_from_locale=es中,但我不明白如何将其应用于此处。我的数组不是承诺数组,是文件数组。好吧,整个事情对我来说仍然是模糊的。
ein
这是我的代码,如果我能看到console.log 消息,它将起作用:
return myArray.reduce(function(previous, current) {
var BYTES_PER_CHUNK = 100000;
var start = 0;
var temp_end = start + BYTES_PER_CHUNK;
var end = parseInt(current.size);
if (temp_end > end) temp_end = end;
var content = ''; // to be filled by the content of the file
var uploading_file = current;
Promise.resolve().then(function() {
return upload();
})
.then(function(content){
// do stuff with the content
Promise.resolve();
});
},0) // I add the 0 in case myArray has only 1 item
//},Promise.resolve()) goes here?
.then(function(){
console.log('ein') // this I never see
});
function upload() {
if (start < end) {
return new Promise(function(resolve){
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;
return Promise.resolve(content);
}
}
几条评论后更新,现在看来它可以工作了……还不确定
var uploading_file, start, temp_end, end, content; var BYTES_PER_CHUNK = 100000;
myArray.reduce(function(previous, current) { return previous .then(function() { BYTES_PER_CHUNK = 100000; start = 0; temp_end = start + BYTES_PER_CHUNK; end = parseInt(current.size); if (temp_end > end) temp_end = 结束;内容 = '';uploading_file = 当前;
upload() .then(function(content){ // do stuff with "content" console.log('here') return Promise.resolve(); });
}); },Promise.resolve()) .then(function(){ console.log('ein'); });
function upload() { if (start < end) { return new Promise(function(resolve){ 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; 返回 Promise.resolve(内容); } }
改进的代码,似乎工作,也许更容易阅读?
var start, temp_end, end; var BYTES_PER_CHUNK = 100000; myArray.reduce(function(previous, current) { return previous .then(function() { start = 0; temp_end = start + BYTES_PER_CHUNK; end = parseInt(current.size); if (temp_end > end) temp_end = end; current.data = ''; return upload(current) .then(function(){ // do other stuff return Promise.resolve(); }); }); },Promise.resolve()) .then(function(){ // do other stuff }); function upload(current) { if (start < end) { return new Promise(function(resolve){ var chunk = current.slice(start, temp_end); var reader = new FileReader(); reader.readAsText(chunk); reader.onload = function(e) { if (e.target.readyState == 2) { current.data += e.target.result; start = temp_end; temp_end = start + BYTES_PER_CHUNK; if (temp_end > end) temp_end = end; resolve(upload(current)); } } }); } else { return Promise.resolve(); } }