Skip to content

Cancel a request #1377

Closed
Closed

Description

@clementbirkle

To avoid duplication of requests (double click), I would be able to stop a running query. I do not see how. Can you help me please ?

let inProcess = false;

return Restangular.withConfig(function(RestangularConfigurer) {
    RestangularConfigurer

        ...

        .addFullRequestInterceptor(function(element, operation, what, url, headers) {

            if (inProcess) {
                // cancel the request here
                return;
            }

            inProcess = true;

        })

        ...
});

Activity

  1. daviesgeek commented on Jun 16, 2016

    @daviesgeek
    Collaborator

    Here's the way I've done it:

    let canceler = $q.defer()
    Restangular.all('users').withHttpConfig({timeout: canceler.promise}).get()
    canceler.resolve() // cancels the request

    This is a little counterintuitive, so let me explain. $http takes a timeout parameter (see the $http docs:

    timeout in milliseconds, or promise that should abort the request when resolved.

    Resolving the promise (canceler in this case), will cancel the request

  2. clementbirkle commented on Jun 24, 2016

    @clementbirkle
    Author

    Really great ! It works well, thank you for your solution :)

  3. added a commit that references this issue on Dec 30, 2016
    8552c51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions