Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chaining angular $timeout

I'm trying to chain calls to AngularJS's $timeout function. I've seen lots of threads on here which allow the chaining of promises, some specifically using angular's $q, but this seems like it should be super simple. I guess I'm missing something obvious. Here's what I'd like to do:

$timeout(firstFunction, firstDelay)
.then($timeout(secondFunction, secondDelay))
.then($timeout(thirdFunction, thirdDelay));

While all three functions get called, the $timeouts all start simultaneously. I can see why this doesn't work, but how do I get what I want? Can I even use promises here? I was previously just arranging the delays so that they cascade, but that seems like more work to maintain...

like image 869
samson Avatar asked Mar 21 '14 23:03

samson


Video Answer


1 Answers

The $timeouts are executing immediately. Wrap them in functions that will be called when each promise resolves...

$timeout(firstFunction, firstDelay)
    .then(function () { return $timeout(secondFunction, secondDelay); })
    .then(function () { return $timeout(thirdFunction, thirdDelay); });
like image 113
Anthony Chu Avatar answered Sep 24 '22 13:09

Anthony Chu