$ Interval function callback

How can I be sure that the function runAfterLoopFinishes()

works after completion $interval

? Right now, it runs right after the first loop definition.

var loop = $interval(function () {
  // Do something great 10 times
}, 1000, 10);

// To run after looping
runAfterLoopFinishes();

      

+3


source to share


2 answers


$interval

returns a promise.

var loop = $interval(function () {
  // Do something great 10 times
}, 1000, 10);

loop.then(function(){
  // To run after looping
  runAfterLoopFinishes();
}

      



An $interval

promise will be notified after each iteration, but only resolved after the last one.

+9


source


if you want to make sure the run function after function b async

will help control it.



async.series([
    function(callback){     // function a
        // do some stuff ...
        callback(null, 'one');
    },
    function(callback){     // function b
        // do some more stuff ...
        callback(null, 'two');
    }
],
function(err, results){
                            // results 
});

      

-1


source







All Articles