Given this code, inside a simple Angular controller:
var s = $timeout(function() {
console.log(s);
}, 2000 );
console.log(s);
$timeout(function() {
$timeout.cancel(s);
}, 1000);
So we're creating a timeout in the variable s
that's supposed to occur in 2 seconds. Then, 1 second into this, we cancel the timeout, so it never occurs. Before we cancel, however, we log s
to the console. We would expect to see its status code to be 1 and its value to be undefined. Instead we see its status to be 2 and its value to be "canceled." Somehow, the system "knows" ahead of time that we're going to be cancelling the timeout and it adjusts accordingly.
What on earth! How is this happening? I would expect the state of object s
to be like this after our $timeout.cancel(s);
call. I certainly do not expect it before. What's going on here?