1

I'm using for loop to send ajax requests to delete multiple items in datatables. Is there any way I can call functions after all the requests are succeeded? Calling functions after every request bring some issues for my datatables, also it's not working if calling functions outside of loop cause they're asynchronous requests

    for(var i = 0; i < $scope.selectedRows.length; i++) {
      $http({
        method: 'DELETE',
        url: $scope.url + '/' + $scope.selectedRows[i].name + '?recursive=true'
      })
      .then(function(res) {
       // $scope.clearSelect();
       // $scope.dtInstance.rerender();
       // $mdDialog.hide();
      }, function(res) {
        console.log('error');
      })
    }
5
  • its bad idea to send multiple request to delete rows, you should consider updating api to accept multiple ids something like DELETE /post?ids=1,3,5,7,8... etc. this way can delete in one request Commented Mar 9, 2016 at 6:42
  • Why don't you make only one ajax request to delete all the record and then in the completion of ajax make another ajax call. Because looping ajax call is not good solution Commented Mar 9, 2016 at 6:43
  • Using a rest call inside a loop is a very bad practice. it should be one function that deletes all on the server side. Commented Mar 9, 2016 at 6:43
  • use $q.all[<promise1>,<promise2> ..] method. It will finish when ALL aync call will finish Commented Mar 9, 2016 at 6:44
  • Thanks for your suggestions guys, I'm doing front end only. I'll discuss this with my colleague if it can't be resolved well at my end Commented Mar 9, 2016 at 6:52

1 Answer 1

3

You should collect the promises of the requests and use the

$q.all(promises).then(...)

Look here: wait for all $http requests to finish

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.