AngularJS + $q, Do something after multiple ajax calls have finished
Asked Answered
P

2

7

I need to load some data on page load and then do execute a task. in order to get the data i want i execute multiple different ajax calls. But in order to execute the task, i need all to make sure that all the ajax calls have finished. Here is what i have done so far:

$q.when(
        $http.get('url1').success(function (data) {
            $scope.data1 = data;
            console.log("ajax1 finished");
        }),
        $http.get('url2').success(function (data) {
            $scope.data2 = data;
            console.log("ajax2 finished");
        }),
        $http.get('url3').success(function (data) {
            $scope.data3 = data;
            console.log("ajax3 finished");
        }),
        $http.get('url4').success(function (data) {
            $scope.data4 = data;
            console.log("ajax4 finished");
        })
    ).then(
        console.log("All ajax calls have finished!"),
        executeTask()
    );

My problem is that the code in block then(...); is not executed after all the ajax call have finished. I get something like this in my console:

ajax2 finished
ajax1 finished
All ajax calls have finished!
ajax3 finished
ajax4 finished

I must be doing something wrong. How can i make it work the way i want?


Edit: I tried the following like it was mentioned in the answers but i still face the same problem.

$q.all([
    $http.get('url1').then(function (data) {
        $scope.data1 = data;
        console.log("");
    }),
    $http.get('url2').success(function (data) {
        $scope.data2 = then;
        console.log("ajax2 finished");
    }),
    $http.get('url3').then(function (data) {
        $scope.data3 = data;
        console.log("ajax3 finished");
    }),
    $http.get('url4').then(function (data) {
        $scope.data4 = data;
        console.log("ajax4 finished");
    })
]).then(
    console.log(""),
    executeTask()
);
Philippi answered 31/7, 2014 at 9:43 Comment(3)
I believe that $q.all should accept an array of promisesSabra
@Sabra tried this also and it doesn't work.Philippi
You still have a success call in your edited post.Postulate
S
15

I've made a working plunker for you with use of $q.all()

http://plnkr.co/edit/JHd3XPTKBxx4KRso5JRB?p=preview

  $q.all([
    $http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
      $scope.ip.one = response.data
      console.log('one')
    }),
    $http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
      $scope.ip.two = response.data
      console.log('two')
    }),
    $http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
      $scope.ip.three = response.data
      console.log('three')
    }),
    $http.jsonp('http://ip.jsontest.com/?callback=JSON_CALLBACK').then(function(response) {
      $scope.ip.four = response.data
      console.log('four')
    }),
  ]).then(function() {
    console.log('all')
  })
Sabra answered 31/7, 2014 at 10:32 Comment(2)
Thanks. The mistake was that i was doing .then(myTask()) instead of .then(function() { myTask() }). It was right in front of my eyes but i missed it.Philippi
Also remember that promise for $http returns a response object that contains few values and one of it is data that you probably wan't to useSabra
M
2

You have two solutions :

Meneses answered 31/7, 2014 at 9:50 Comment(1)
check my edit. I tried using $q.all but it still doesn't work the way i want.Philippi

© 2022 - 2024 — McMap. All rights reserved.