javascript - AngularJS promises, how to $q.first.then(others[]) -
in angularjs controller need ensure paramount variable initialized before performing other tasks.
var firstpromise = $scope.watch("myparamount"...); // ng-init var otherpromises = []; // once obtained myparamount, others // this?! $q.firstpromise.then.all(otherpromises).then(function(){ console.log("first, otherpromises completed!"); })
how fix "fake" code?
assuming actual promises, should able use promise chaining this.
here's sample using timeouts illustrative purposes:
var firstpromise = $timeout(echo('first'), 1000); firstpromise.then(function(data){ console.log(data); // 'first' return $q.all([ // other promises $timeout(echo('other 1'), 1000), $timeout(echo('other 2'), 500), $timeout(echo('other 3'), 1500) ]);; }).then(function(data){ console.log(data); // ['other 1', 'other 2', 'other 3'] }); function echo(v) { return function(){ return v; } }
that 1 way chain them, other promises isn't run until first has resolved.
Comments
Post a Comment