I want to run the script after the foreach loop complete its execution,
// Code goes here
var app=angular.module('test',[]);
app.controller('testCtrl',function($scope,$timeout,$q){
var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
$timeout(function () {
console.log('foreach running');
log.push(key + ': ' + value);
}, 50000);
});
$q.all(log).then(function(){
console.log(log);
})
})
In the above code the log array values are created in forEach loop I want to print the values in log array after the foreach execution completed, how should I do it?
Edit
when I used $timeout inside angulr.forEach the console.log fired before the completion of angular.forEach cycle, why so? where can I refer whether the call is synchronous or asynchronous?