Reputation: 15239
In an AngularJs controller I need to ensure a paramount variable initialized before performing other tasks.
var firstPromise = $scope.watch("myParamount"...); // from ng-init
var otherPromises = []; // once obtained myParamount, do others
// something like this?!
$q.firstPromise.then.all(otherPromises).then(function(){
console.log("first, then otherPromises completed!");
})
How to fix this "fake" code?
Upvotes: -1
Views: 53
Reputation: 5684
Assuming those are actual promises, you should be able to use promise chaining to do something like this.
Here's a sample using timeouts for 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 is one way to chain them, so the other promises isn't run until the first has resolved.
Upvotes: 1