-1
  $scope.$on("stopSpinner", function() {
        $('body').mask('hide');
    });

the above code works that when stopSPinner is event is recieved, it executes. But my requirement is little more involved. I want to wait for two different events to recive before executing a function. Order of events could be different but i want to function to only execute once both events have occured. how can this be achived

josh_boaz
  • 1,963
  • 7
  • 32
  • 69

1 Answers1

1

Use $q.all and promises, like this:

var deferred1 = $q.defer();
$scope.$on("stopSpinner", function() 
{ 
    $('body').mask('hide'); 
    deferred1.resolve();
 });

var deferred2 = $q.defer();
$scope.$on("event2", function() 
{ 
    $('body').mask('hide'); 
    deferred2.resolve();
 });

$q.all([deferred1, deferred2]).then(
function(){
     //your  code after the 2 events...

 })

This way, you'll be able to wait for 2 async task to complete in a single function.

illeb
  • 2,942
  • 1
  • 21
  • 35