the following uploading function is the function encapsulated into the upload form of Promise:
function uploading(type,opts,serverurl,callback) {
return new Promise(function (resolve,reject) {
var formdata = new FormData();
formdata.append(type,JSON.stringify(opts));
$.ajax({
type: "post",
url: serverurl,
data: formdata,
cache : false,
processData : false, // dataFormdata
contentType : false, // Content-type
success: function (data,success) {
callback&&callback(JSON.parse(data));
console.log("look here")
console.log(data);
resolve(data)
},
error: function (err) {
reject(err);
alert("");
}
})
})
}
the main code starts with the following:
the code logic related to this problem is mainly an array of Promise objects generated by: (chunksarrpromise for calling the uploading function, Promise.all (chunksarrpromise). Then (values) = > {}, waiting for all the file blocks to be processed to perform the corresponding operation. The problem now is that the values output in the above Promise.all (chunksarrpromise). Then is always an empty array. And every promise in it does have a return after execution. )
var chunksarrpromise = new Array();
for(let curindex = 0;curindex < chunks;curindexPP) {
if(file.size-start <= chunkSize) {
end = file.size;
}else {
end = start + chunkSize;
}
//
if(!isinArray(alreadychunks,curindex)) {
let tempcell = {
data: null,
n: 0,
file_id: summary,
username: username,
length: 0
};
tempcell.length = end - start;
filder[curindex] = new FileReader();
filder[curindex].readAsText(file.slice(start, end),"gb2312");
filder[curindex].onload = function () {
tempcell.n = curindex;
tempcell.data = filder[curindex].result;
console.log(tempcell)
//chunk
chunksarrpromise.push(uploading("chunks", tempcell, chunksurl));
}
}
start = end;
}
console.log(chunksarrpromise);
Promise.all(chunksarrpromise).then((values) => {
console.log("Promise %%%% all")
console.log(values);
}).catch(err => {
console.log(err);
})
the result of code execution is as follows:
(the first look here is because I still have a function to call uploading above. The real Promise.add starts from Promise% all, and you can see that what it prints out is [], and the data after the real execution of its Promise is mochen, that is to say, the Promise.all has not waited for its internal Promise to be executed. Have you ever encountered it? What are we going to do about it? )
Code address: https://github.com/guangying1.