7

Have a question about synchronizing nested promises when using $q in Angular. Will the following code ensure that the entire chain of promises is waited for? Meaning will the nested calls to services that return promises be waited for in the $q.all block?

var call1 = service1.get('/someUr').then(function(){
  return service2.get('/someUrl2'); //returns promise
});

var call2 = service3.get('/someUr').then(function(){
  return 'hello';
});

var call3 = service4.get('/someUr').then(function(){
  return service3.get('/someUrl3');//returns promise
});

$q.all(call1,call2,call3).then(function(){
  console.log('All asynch operations are now completed');
});

Basically: Is there a chance with the current code that the then of $q.all will execute before all the nested promises are resolved? Or is it recursive?

TGH
  • 38,769
  • 12
  • 102
  • 135
  • 1
    No, I don't think so. The `$q.all()` will wait for `call1`, `call2` and `call3` to resolve together before triggering the `then()` function, but the nested async calls returned from `call1` and `call3` will not be waited on. – Brett Oct 03 '14 at 18:34
  • I was under the impression that it would wait. – Kevin B Oct 03 '14 at 20:28
  • 1
    I don't have an angular env handy to test that at the moment, but with native promises, it definitely does wait. http://jsfiddle.net/p4evLjm6/ – Kevin B Oct 03 '14 at 20:34
  • @KevinB Looks like you are correct. I created a quick Angular test bellow. – TGH Oct 06 '14 at 20:46

1 Answers1

5

Yes it looks like Kevin is correct. I created a quick test in angular as well to confirm the behavior.

angular.module('myModule').controller('testController', function ($q,$scope) {

  function promiseCall(data,timeout) {
    var deferred = $q.defer();

    setTimeout(function() {
      deferred.resolve(data);
      console.log(data);
    }, timeout);

    return deferred.promise;
  }

  var a = promiseCall('call1 a',1000).then(function(){
    return promiseCall('call2 a',50);
  });

  var b = promiseCall('call1 b',500);

  var c = promiseCall('call1 c',1000).then(function(){
    return promiseCall('call2 c',50).then(function(){
      return promiseCall('call3 c',6000);
    });
  });

  $q.all([a,b,c]).then(function(res){
    console.log('all calls are done');
  });

});
TGH
  • 38,769
  • 12
  • 102
  • 135