AngularJS $q. Deferred queue

僤鯓⒐⒋嵵緔 提交于 2019-11-28 03:37:35

问题


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?


回答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


来源:https://stackoverflow.com/questions/18943024/angularjs-q-deferred-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!