0

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?

2 Answers 2

1
var values = {name: 'misko', gender: 'male'};
  var log = [];
  var promises = [];
  angular.forEach(values, function(value, key) {
   var defer = $q.defer();
   promises.push(defer.promise);
   $timeout(function () {
       console.log('foreach running');
       log.push(key + ': ' + value);
       defer.resolve();
    }, 50000);
  });
  $q.all(promises).then(function(){
    console.log(log);  
  })
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, please can you explain little, can we achieve the result without using variable promises and I want to know is angular.forEach synchronous call or asynchronous call? $timeout is asynchronous call right?
angular.forEach is sync. $timeout is async. You can do it without variable 'promises' - you can do pretty wahtever, but it wont be better code.
Thanks a lot, when I used $timeout inside angulr.forEach the console.log fired before the angular.forEach cycle completion why so? where can I refer whether the call is synchronous or asynchronous?
1

since you have no asynchronized code running no need for $q so you can simply put console.log(log) after the loop, it will certainly be running after the loop is finished and the code would be like that

var app=angular.module('test',[]);
app.controller('testCtrl',function($scope,$timeout,$q){
  var values = {name: 'misko', gender: 'male'};
  var log = [];
  for (var key in values) {
    log.push({[key]:values[key]});
  };
  console.log(log);
});

3 Comments

Is $http calls only the asynchronus call? , I want to know what will be the result if I have the huge value in values object it will take time to iterate the object, is it? while that call is running console log could be called before that execution right? or will it be the synchronous call? I have used the angular.forEach to iterate the values does angular.forEach is an asynchronous call or not?
angular.forEach is synchronous function for sure
Thanks for your response, Where can we refer this? if its synchronous call the console.log should fire after the execution of angular.forEach but when I used $timeout inside angulr.forEach the console.log fired before the angular.forEach cycle completion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.