Block http requests before specific request is executed?

225 views
Skip to first unread message

Dries De Smet

unread,
May 28, 2014, 10:15:22 AM5/28/14
to ang...@googlegroups.com
Hi,

Is it possible in AngularJS to wait before a specific request is done?

I'm executing a $http.get() in the run() block of my authentication module to retrieve an access token. After I receive this token, I set the restangular default headers so that future requests can use it

Since the run() doesn't wait for the response, it can be that my index page is already requesting data without providing the token.

I want to avoid adding checks in every route or service call, so I was curious if I can block other $http requests until the token one is done.

On the IRC channel they referred me to http interceptors, but I'm not sure how to use them in this case to achieve what I want. (I already use it to handle 401 errors, but I don't see how I can block other requests with it).

Thanks in advance,

Dries

Tandon, Rishi

unread,
May 28, 2014, 10:42:40 AM5/28/14
to ang...@googlegroups.com
Check for Angular promise and deferred objects.
--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, 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.


--
Rishi Tandon
Pearson Learning Technology Group

Mobile: (310) 926-9032

Pearson
Always Learning
Learn more at www.pearson.com

Dries De Smet

unread,
May 28, 2014, 10:48:41 AM5/28/14
to ang...@googlegroups.com
How can I use a promise in this case to prevent other requests to be executed? As far as I could test, the new request will still be fired.

Op woensdag 28 mei 2014 16:42:40 UTC+2 schreef Rishi Tandon:
Check for Angular promise and deferred objects.

On Wednesday, May 28, 2014, Dries De Smet <dries...@gmail.com> wrote:
Hi,

Is it possible in AngularJS to wait before a specific request is done?

I'm executing a $http.get() in the run() block of my authentication module to retrieve an access token. After I receive this token, I set the restangular default headers so that future requests can use it

Since the run() doesn't wait for the response, it can be that my index page is already requesting data without providing the token.

I want to avoid adding checks in every route or service call, so I was curious if I can block other $http requests until the token one is done.

On the IRC channel they referred me to http interceptors, but I'm not sure how to use them in this case to achieve what I want. (I already use it to handle 401 errors, but I don't see how I can block other requests with it).

Thanks in advance,

Dries

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+unsubscribe@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.

Eric Eslinger

unread,
May 28, 2014, 11:12:11 AM5/28/14
to ang...@googlegroups.com
Personally, I use a modified version of Witoldz's angular-auth module:


Basically, intercept all the 401s and stash them, and when the token's been generated, resolve all the 401'd data by re-requesting with the token.

e


To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages