I was recently tackling a project in AngularJS 1.5.3, and I've run into some difficulties with chaining promises.
Here's a snippet of the function causing me trouble:
this.login = function(username, password) {
var promise = $auth.login(username, password).then(...);
return promise;
}
this.tests = [
['I am the LoginController, responsible for user login'],
['I have a logs attribute to bind the current state to the view', null, function() { return !!angular.isArray(self.log); }],
['I make use of the $auth attribute to expose the $auth service', function() { return !!(self.$auth === $auth); }],
['I can retrieve tokens from the server', null, function() { return self.login({username: 'example', password: '1234'}); }, function() { return !!($auth.currentUser.id === 1 && $auth.currentUser.hasValidToken() === true); }]
];
You can probably grasp my intentions here...
The array structure is as follows:
[
0 => string,
1 => a function that returns a promise,
2 => a function that verifies the impact of the preceding function
]
I had the idea of building a directive to automatically test controllers with visual cues - regardless -
What I require is to iterate through the array, execute them sequentially, and return true/false based on [1,2];
Initially, I tried directly within an Angular1 template using ngRepeat
<ul>
<li ng-if="$last" ng-repeat="test in tests">{{test[0]}} :: {{test[1]() && test[2]()}}</li>
</ul>
This approach failed miserably since they weren't executed in order. It dawned on me that I needed to wrap them in promises?