how to view http pending requests ($http.pendingRequests)

2,428 views
Skip to first unread message

zilabify

unread,
Dec 11, 2013, 1:42:55 AM12/11/13
to ang...@googlegroups.com
Hi Guys,

$provide.factory('myHttpInterceptor', function($q, $http) {
  return {
    // optional method
    'request': function(config) {
      // do something on success
      console.log(config);
      console.log($http.pendingRequests);
      return config || $q.when(config);
    }
  }
});

$httpProvider.interceptors.push('myHttpInterceptor');


just wondering what should I inject to the property to view the http object?

Sander Elias

unread,
Dec 11, 2013, 3:14:20 AM12/11/13
to ang...@googlegroups.com
Hi Zilabify,

your building an interceptor, so you want to know what other requests are going on during a in process $http request?
Your current solution is to try to inject $http, to the inside of $http. That's a circular reference, and is not going to work.
What is it you are trying to accomplish? I have looked, and didn't find an easy way to get hold of the http object inside a interceptor. 
I think you better use an decorator for what you need. (my guess, canceling pending request upon a new one?)

something like:
        $provide.decorator('$http', function($delegate) {
            // $delegate is the original $http!
            console.log($delegate.pendingRequests)
            return $delegate
        });

Regards
Sander Elias

zilabify

unread,
Dec 11, 2013, 6:18:37 AM12/11/13
to ang...@googlegroups.com
Hi Sander, 

Yes, here's a working plunk.

I manage to inject the http by injecting the $injector object, now problem is, how do I simulate pending requests? I always see a blank array.

What I want for the end product is, if there's a request that's looks like a duplicate http request, then cancel it.

--
Chris

Sander Elias

unread,
Dec 11, 2013, 6:40:53 AM12/11/13
to ang...@googlegroups.com
Hi Zilabify,

launch a couple of requests, and make your backend pause a bit on each of them.
Or whip up some tests, and use that to test it!

Regards
Sander


zilabify

unread,
Dec 22, 2013, 1:21:31 PM12/22/13
to ang...@googlegroups.com
I tried adding sleep in php before returning a request - doesn't work. 
Any help with an actual code is welcome.

Sander Elias

unread,
Dec 23, 2013, 3:13:17 AM12/23/13
to ang...@googlegroups.com
Hi Zilabify,

an sleep(int $seconds) in your PHP should do the trick.

regards
Sander

zilabify .

unread,
Dec 25, 2013, 12:44:26 PM12/25/13
to ang...@googlegroups.com
it doesn't


--
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/d1hEdHri51U/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/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages