AngularJS $q. Deferred queue
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?
Solution 1:
Don't use a queue and that boolean flag, just have one variable storing a promise representing all uploads. Also, your upload
function should not take a Deferred object to resolve as an argument, but simply return a new promise.
Then the addAnUpload
becomes as simple as
var allUploads = $q.when(); // init with resolved promise
function AddAnUpload(file) {
allUploads = allUploads.then(function() {
return upload(file);
});
}
With the closure, you don't need that queue
to store the waiting uploads any more. If you want allUploads
to fulfill always, even if one upload
fails, you need to return an always-fulfilling promise from the then
-callback:
return upload(file).then(null, function(err) {
console.log(err, "does not matter");
}); // fulfills with undefined in error case