jsFiddle Example: Proper Timeout Handling with AngularJS

AngularJS has a really poor handling of timeouts, at least for complex HTTP calls. You can provide a timeout in milliseconds, but if the request does time out, you just get a cancelled request. Now this might be fine for simple situations, but when you have CORS Ajax calls, you end up not being able to tell a timeout from a CORS request failure (at least not easily). This can be handled by switching to setting the timeout with a Promise instead. This code shows how to use a promise for your timeout in AngularJS to get clear error messages.

The createData() function is purely to work with jsFiddle's echo API.

 

Comments

Dayanand Saraswati's Gravatar This is good stuff, but the only problem is that I don't want to hard code the timeout of the request, because I am not sure whether the timeout will be sufficient or not. I want to handle default timeout as set by the request
Mohamed Benseghir's Gravatar Usefull.
God Bless You!
metz2000's Gravatar Where would timedOut be set to true in a real app when timeout is not controlled by caller but happens in http object?
Comments are not allowed for this entry.
Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.