Currently, I am working on implementing a $q.all function to execute multiple functions and then collect all the outputs in a function connected to the .then method at the end.
Even though it seems like the promises are being called in the correct sequence and the $all .then method is being executed at the end, the results variable is returning an array of null values (one for each promise in the $q.all).
You can find the JS Fiddle example at http://jsfiddle.net/QqKuk/120/, and I am using angular 1.0.1.
Below is a simplified version of the code I have:
This is my basic HTML structure, displaying some debug text and output:
<div ng-controller="MyCtrl">
<p>{{fromThen}}</p>
<p>{{fromThen2}}</p>
<p>{{runOrder}}</p>
</div>
And here is my controller, where logOne, logTwo, and logThree are not identical functions:
var myApp = angular.module('myApp', []);
function MyCtrl($scope, $q, $timeout) {
var logOne = function(value) {
$scope.fromThen = $scope.fromThen + value;
var deffered = $q.defer();
deffered.promise.then(function() {
$scope.runOrder = $scope.runOrder + '.logOne()';
$scope.fromThen = $scope.fromThen + value.toUpperCase();
deffered.resolve(value);
return deffered.promise;
});
deffered.resolve();
};
var logTwo = function(value) {
$scope.fromThen = $scope.fromThen + value;
var deffered = $q.defer();
deffered.promise.then(function() {
$scope.runOrder = $scope.runOrder + '.logTwo()';
$scope.fromThen = $scope.fromThen + value.toUpperCase();
deffered.resolve(value);
return deffered.promise;
});
deffered.resolve();
};
var logThree = function(value) {
$scope.fromThen = $scope.fromThen + value;
var deffered = $q.defer();
deffered.promise.then(function() {
$scope.runOrder = $scope.runOrder + '.logThree()';
$scope.fromThen = $scope.fromThen + value.toUpperCase();
deffered.resolve(value);
return deffered.promise;
});
deffered.resolve();
};
$scope.fromThen = '';
$scope.fromThen2 = 'No Value';
$scope.runOrder = '';
$q.all([logOne('One'), logTwo('Two'), logThree('Three')])
.then(function(results) {
$scope.runOrder = $scope.runOrder + '.then';
$scope.fromThen2 = results;
});
}
The current output displays:
OneTwoThreeONETWOTHREE [null,null,null] .logOne().logTwo().logThree().then
It appears that the order of execution is correct, so I am puzzled as to why I am receiving null values in the result. Could it be that I am incorrectly using defer.resolve(value)?
I have reviewed other examples but have not been able to identify the issue causing the lack of a result.
Any assistance you can provide would be greatly appreciated. As this is my first post, any suggestions on additional information to include (or exclude) would also be helpful.
Thank you. Neil