javascript - Angular $q Service - Limiting Concurrency for Array of Promises -


might give bit of background context problem: i'm building angular service facilitates uploading chunks of multipart form data (mp4 video) storage service in cloud.

i'm attempting limit number of unresolved promises (put requests of chunk data) happening concurrently. using $q.all(myarrayofpromises).then()... listen chunk upload promises being resolved, , return asynchronous call (post complete file) when happens. think i'm encountering race condition algorithm, because $q.all() gets called before jobs have been scheduled files lot of chunks, succeeds smaller files.

here's alogorithm.

var uploadinchunks = function (file) {    var chunkpromises = [];    var chunksize = constants.chunk_size_in_bytes;    var maxconcurrentchunks = 8;    var startindex = 0, chunkindex = 0;    var endindex = chunksize;    var totalchunks = math.ceil(file.size / chunksize);    var activepromises = 0;     var queuechunks = function () {       while (activepromises <= maxconcurrentchunks && chunkindex < totalchunks) {          var deferred = $q.defer();          chunkcancelers.push(deferred); // array broader scope can use cancel uploads they're happening           var fileslice = file.slice(startindex, math.min(endindex, file.size));           chunkpromises.push(addchunkwithretry(webupload, chunkindex, fileslice).then(function () {            activepromises--;            queuechunks();         });          activepromises++;         startindex += chunksize;         endindex += chunksize;         chunkindex++;      }   }    queuechunks();    return $q.all(chunkpromises).then(function () {      return filesapi.completefile(file.fileid);   }); }; 

even though $q.all called prematurely, chunks of file still pending / not scheduled @ time executed , resolved successfully.

i've done fair amount of reading throttling concurrency of $q , know there libraries out there assist, i'd have understanding of why not work of time :)

the promise you're returning ($q.all) isn't indicative of promise want return. in code above, returned promise finish after first maxconcurrentchunks resolved, because that's how many promises in chunkpromises when pass $q.all().

another way handle (and result want) following psuedocode:

var uploadinchunks = function(file){      //...vars...     var filecompletedeferral = $q.defer();      var queuechunks = function(){         chunkpromises.push(nextchunk(chunkindex).then(function () {             activepromises--;              if(allchunksdone()) { //could activepromises == 0, or chunkindex == totalchunks - 1                 filecompletedeferral.resolve();             }             else {                 queuechunks();             }         });     }      return filecompletedeferral.promise.then(completefile()); } 

the promise returned code resolve after promises done, rather first 8.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -