angular promise - angularjs chain http post sequentially -


in application, storing data in local storage , trigger async http post in background. once posted, posted data gets removed local storage. when http post in process, there may more data added local storage need queue post , sequentially process because, need wait local storage cleared successful posts. task should called recursively until there data in local storage.

taskqueue: function () {     var deferred = $q.defer();     queue.push(deferred);     var promise = deferred.promise;      if (!saveinprogress) {       // post data storage       var promises = queue.map(function(){$http.post(<post url>, <post data>).then(function(result){           // clear successful data           deferred.resolve(result);       }, function(error){          deferred.reject(error);       })     })      return $q.all(promises); } 

as angular newbie, having problems above code not being sequential. how can achieve intend to? queue length unknown , queue length increases process in progress. please point me other answers if duplicate.

async.js sounds idea if don't want use library ...

$q.all batch array of requests , fire them @ same time , resolve when promises in array resolve - not want.

to make $http calls sequentially array ....

var request0 = $http.get('/my/path0'); var request1 = $http.post('/my/path1', {data:'fred'}); var request2 = $http.get('/my/path2'); var requestarray = []; 

then ...

requestarray.push(request0); requestarray.push(request1); requestarray.push(request2); 

then ...

requestarray[0].then(function(response0) {     // response0     return requestarray[1]; }).then(function(response1) {     // response1     return requestarray[2]; }).then(function(response2) {     // response2 }).catch(function(failedresponse) {     console.log("i displayed when request fails (if ever)", failedresponse) }); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -