I was working on AngularJS 1.5.3 project, and I'm having trouble with chaining promises.
I have a function that looks like that;
this.login = function(u,p){ var promise=$auth.login(u,p).then(...); return promise;}
this.tests = [
['Im LoginController, i let users login'],
['I have logs attr, to bind current state to view',null,function(){return !!angular.isArray(self.log)}],
['I have $auth attr, that expse the $auth service',function(){return !!(self.$auth===$auth)}],
['I can get tokens from server',null,function(){return self.login({u:1,pass:1234})},function(){return !!($auth.currentUser.id === 1 && $auth.currentUser.hasValidToken()===true)}
];
I think you can see what I'm trying here...
So the array is composed of
[
0 => string
1 => a function that return promise
2 => a function that confirms that the previous function effects took place
]
I wanted to create a directive to autotest controllers presenting visual aids- anyhow-,
What I need is to loop the array and execute them in order and return true/false for [1,2];
My first attempt was directly into Angular1 template using ngRepeat
<ul>
<li ng-if="$last ng-repeat="test in tests">{{test[0]}} :: {{test[1]() && test[2]()}}</li>
</ul>
Failed very bad, because of course they don't execute in order. So I realized I need to wrap them in promises ?
testsis an array then it will iterate them in what ever order the array is in, if its iterating an object thats when the order can't be guaranteed. However with an ng-repeat they will all execute pretty much at the same time. Have a look at $q.all if thats desired as this will do it all in code rather than relying on angular bindings. Would make it easier to test.