我有数组(它是文件队列):
[{deferred: fileDef, data: file}, {...}, ...]
每个fileDef和文件都发送到上传函数,上传函数返回fileDef.promise并在上传后调用fileDef.resolve或fileDef.reject。
我想按顺序上传文件:加载前一个文件后上传下一个文件。
现在我用
var queue = [];
var uploading = false;
//file input callback call each time the user selects files
function addAndUpload (file) {
queue.push({deferred: $q.defer(), data: file});
if (!uploading) recurceQueue();
function recurceQueue () {
if (queue.length) {
uploading = true;
var fileObj = queue.shift();
upload(fileObj.deferred, fileObj.data);
fileObj.deferred.promise.finally(function () {
uploading = false;
recurceQueue();
})
}
}
}
但这似乎很糟糕。如何写得更好?