I have array (f.e. it is queue of files):
[{deferred: fileDef, data: file}, {...}, ...]
Each fileDef and file send to upload function which return fileDef.promise and call fileDef.resolve or fileDef.reject after uploading.
I want upload files in order: next file upload after previous file is loaded.
Now I use
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();
})
}
}
}
But it seems bad. How to write better?