AngularJS Array of Promises -


can me angular promises? have following functions should take in array of file objects, iterate on them, , upload each one. during each iteration, promise object pushed array of promises. within upload function have cycle function .then() attached, should not called until promise objects have resolved. think code looks correct, it's not working right. images upload, cycle(files).then() being called immediately, rather once promises array resolves.

function upload(files) {     var uploadcount = files.length;     function cycle(files) {         var promises = [];         (var = 0; < files.length; i++) {             var deferred = $q.defer();             promises.push(deferred);             var file = files[i];             upload.upload({                 url: '/photos.json',                 file: file             }).success(function(){                 $scope.progresscurrentcount += 1;                 deferred.resolve();             });         };         return $q.all(promises);     };      cycle(files).then(function(result) {         if(uploadcount > 1) {             $scope.lastaction = 'uploaded ' + uploadcount + ' photos';         } else {             $scope.lastaction = 'uploaded ' + uploadcount + ' photo';         }         $scope.showsuccessmodal = true;         $scope.uploading = false;         $scope.faileduploads = [];         $scope.newphotos = {             token: $scope.token,             files: undefined         };         photouploadbtn.removeclass('disabled');     }) }; 

final working code*

rather setting var deferred = $q.defer(); pushing deferred.promise promises array, , resolving deferred in .success() callback, wasn't working, push upload.upload() function without .success() callback promises, , pass $q.all(), lifting.

function upload(files) {     var uploadcount = files.length;     function cycle(files) {         var promises = [];         (var = 0; < files.length; i++) {             var file = files[i];             var promise = upload.upload({                 url: '/photos.json',                 file: file             });             promises.push(promise);         };         return $q.all(promises);     };      cycle(files).then(function(result) {         if(uploadcount > 1) {             $scope.lastaction = 'uploaded ' + uploadcount + ' photos';         } else {             $scope.lastaction = 'uploaded ' + uploadcount + ' photo';         };         $scope.showsuccessmodal = true;         $scope.uploading = false;         $scope.faileduploads = [];         $scope.newphotos = {             token: $scope.token,             files: undefined         };         photouploadbtn.removeclass('disabled');         getphotos(q);     }) }; 

you must push promise, not deferred, promises array:

promises.push(deferred.promise); 

it helps if think this:

  • the promise read-only object want return consumers
  • deferred modifier of read-only promise , want keep yourself

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -