Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs, wait for a nested promise

I have 3 services that return 3 promises, but the third needs the data from the second, so I call it inside the second. I want to wait for all the three promises to be solved, this is the way that I implemented, but doesn't work (waits only for the first and the second).

var promise1, promise2, promise3;

promise1 = service1();
promise2 = service2();

promise2.then(function (data) {
  promise3= service3(data);

});

$q.all([ promise1, promise2, promise3]).then(function success() {
 //somehing
});
like image 685
Tres Avatar asked Jul 31 '14 18:07

Tres


1 Answers

You can assign the second promise's then() callback with a returned promise from the third service.

var promise1, promise2, promise3;

promise1 = service1();
promise2 = service2();

promise3 = promise2.then(function (data) {
  return service3(data);
});

$q.all([ promise1, promise2, promise3]).then(function success() {
 //somehing
});
like image 133
ryeballar Avatar answered Nov 01 '22 06:11

ryeballar