$timeout first argument

140 views
Skip to first unread message

A Matías Quezada

unread,
Aug 17, 2015, 6:08:31 AM8/17/15
to AngularJS
Hey guys, I have a project with heavy promises use where sometimes I use $timeout to get set a timer for my calls (this is pseudocode just for the example, please don't focus on it)

firstPromise([
  $http
.get('/foo/bar'),
  $timeout
(angular.noop, 1500),
]).then(function() {
 
// ...
});

I saw in documentation $timeout arguments are optional but if I pass no function when timer arrives I get "TypeError: fn is not a function".

> $timeout(1000)
< Promise {$$state: Object, $$timeoutId: 187}
app-bootstrap.js:16 TypeError: fn is not a function
    at angular.js:16299
    at completeOutstandingRequest (angular.js:4924)
    at angular.js:5312(anonymous function) @ app-bootstrap.js:16(anonymous function) @ angular.js:16302completeOutstandingRequest @ angular.js:4924(anonymous function) @ angular.js:5312
 
> $timeout()
< Promise {$$state: Object, $$timeoutId: 188}
app-bootstrap.js:16 TypeError: fn is not a function
    at angular.js:16299
    at completeOutstandingRequest (angular.js:4924)
    at angular.js:5312

My question is: as $timeout returns a promise can we use argument type detection to use angular.noop if first argument is a number?

$timeout(10000); // equals to $timeout(angular.noop, 1000)

Which will allow us to chain this promise with others in a more readable way.

What do you think?

Sander Elias

unread,
Aug 17, 2015, 6:50:12 AM8/17/15
to AngularJS
Hi A Matias,

You already can use $timeout(duration).then(doSomething). You can try this for yourself. open up the developers console in your browser and type:
var t = angular.injector(['ng'])
var to = t.get('$timeout')
to
(2000).then(function(){console.log('hi')}))

the result will be:
Promise {$$state: Object}

and 2 seconds later:
hi


(well at least it did in my browser ;)

Regards
Sander


A Matías Quezada

unread,
Aug 17, 2015, 7:23:05 AM8/17/15
to AngularJS
Thanks Sander that's what I did, I pasted the output in the email.

If you see a different output then maybe my version is outdated: "1.3.15"

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/AuJgZfNEBms/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

A Matías Quezada

unread,
Aug 17, 2015, 7:26:04 AM8/17/15
to AngularJS
Actually my version works like yours, the functionality is fine, but plus to that it also sends an error to "$exceptionHandler" factory where real errors go.
Here is where "TypeError: fn is not a function" is thrown.
Reply all
Reply to author
Forward
0 new messages