I'm using $resource in my project. Making POST request and calling api. But after 30 seconds I want to kill the http request. How can I achieve that ?
Asked
Active
Viewed 585 times
0
-
Possible duplicate of [Angularjs how to cancel resource promise when switching routes](http://stackoverflow.com/questions/24440177/angularjs-how-to-cancel-resource-promise-when-switching-routes) – Okazari Dec 11 '15 at 14:24
2 Answers
0
According to the docs, you can specify a timeout parameter on the $resource
actions. I'm assuming it would look something like this (since you specified a POST request I guess you must be calling .$save
or Resource.save
):
$resource('/foo/bar/:id',{id :'@id'},{
save: {method: 'POST', timeout: 30000} // 30 seconds in milliseconds
});
Note that the timeout
parameter can be:
timeout in milliseconds, or promise that should abort the request when resolved.
Haven't tested this at all but hopefully it can help you.

jsonmurphy
- 1,600
- 1
- 11
- 19
0
We can use the cancellable: true in the query object so that request can be cancelled at any point.
var request = $resource('/foo/bar/:id',{id :'@id'},{
query: {method: 'get', isArray: true, cancellable: true}});
var apiCall = request.query({location: destination});
apiCall.$cancelRequest();

Monika
- 31
- 2