angularjsangular-promise

angularjs chain http post sequentially


In my application, I am storing data in local storage and trigger async http post in the background. Once successfully posted, the posted data gets removed from local storage. When http post is in process, there may be more data added to local storage so I need to queue up the post and sequentially process it because, I need to wait for the local storage to be cleared from the successful posts. The task should be called recursively until there is data in local storage.

taskQueue: function () {
    var deferred = $q.defer();
    queue.push(deferred);
    var promise = deferred.promise;

    if (!saveInProgress) {
      // get post data from 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, I am having problems with the above code not being sequential. How can I achieve what I intend to? The queue length is unknown and also the queue length increases as the process is in progress. Please point me to other answers if this is a duplicate.


Solution

  • Async.js sounds a good idea but if you don't want to use a library ...

    $q.all will batch up an array of requests and fire them at the same time and will resolve when ALL promises in the array resolve - WHICH IS NOT WHAT YOU WANT.

    to make $http calls SEQUENTIALLY from an array do this ....

    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) {
        // do something with response0
        return requestArray[1];
    }).then(function(response1) {
        // do something with response1
        return requestArray[2];
    }).then(function(response2) {
        // do something with response2
    }).catch(function(failedResponse) {
        console.log("i will be displayed when a request fails (if ever)", failedResponse)
    });