Last Updated: February 25, 2016
·
1.612K
· dey

$q.when() is the 2nd thing to know in promise world

The 1st thing to know is how to deal with angular's promises.

And the second is you can mix sync/async javascript libs :

var results = {};
$q.when('hello world').then(function(resolved) {
        results.hello = resolved;
}));

So chaining or combine $q.all() is easy :

function sum (a, b) {
      return a + b;
 }

$q.when(sum(1, 2)).then(function(resolved) {
      return $anotherPromiseFunction(resolved);
})).then(function(resolved) {
      alert(resolved);
}));

//or shortly
$q.when(sum(1, 2)).then($anotherPromiseFunction).then(function(resolved) {
      alert(resolved);
}));

cf. [api on angularjs doc][1]

[1]: https://docs.angularjs.org/api/ng/service/$q