Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to break promise chain in interceptors? #715

Closed
Shu-Ji opened this issue Feb 19, 2017 · 3 comments
Closed

how to break promise chain in interceptors? #715

Shu-Ji opened this issue Feb 19, 2017 · 3 comments

Comments

@Shu-Ji
Copy link

Shu-Ji commented Feb 19, 2017

How to break promise chain?

instance.interceptors.response.use((response) ->
    # my server returns {"status": "success", "data": ...}
    # or {"status": "fail", "data": ...}
    server_response = response.data
    if server_response.status == 'fail'
        alert(server_response.data)  # global alert when status == 'fail'
        # this will throw a "> Uncaught (in promise) ..."
        # how can i do to prevent/stop it enter into the next then()?
        # only when server_response.status == 'success' enter the `then` function
        return Promise.reject(response)

    # only status == 'success' will reach here:
    return response

# in my button actions
instance
    .post('accounts/login', account)
    .then (response) ->
        # if i don't use Promise.reject() in the interceptors,
        # everytime i will use a if:
        if response.data.status == 'success'
            doThings(response)

        # but i want this
        doThings(response)

in angular:
http://blog.zeit.io/stop-a-promise-chain-without-using-reject-with-angular-s-q/

in bluebird:
http://openmymind.net/Cancelling-Long-Promise-Chains/

the above examples break chains in then(), can axios break it also in interceptor, somthing likes:

instance.interceptors.response.use((response) ->
    if someCondition(response)
        return null  # break the chain
    else
        return response
@Shu-Ji Shu-Ji changed the title how to prevent response in interceptors? how to break promise chain in interceptors? Feb 19, 2017
@rubennorte
Copy link
Member

I don't recommend you doing so, but if you finally want it you can return a never-resolving promise in your interceptors. E.g.:

instance.interceptors.response.use((response) => {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

@sccovey
Copy link

sccovey commented Jul 29, 2019

I didn't like the idea of using a promise that never resolves or rejects, so I opted to use bluebird's cancellation to do something like this:

axios.interceptors.response.use(null, error => {
    let promise = new Promise(resolve, reject) => {
        setTimeout(() => {
            <code>
            promise.cancel()
        })
    })
    return promise
})

the setTimeout allows the promise to initalize itself so that a proper promise is still returned from the interceptor and .cancel() can be called after the fact

@verbart
Copy link

verbart commented Nov 5, 2019

Reply to @rubennorte:

I don't recommend you doing so, but if you finally want it you can return a never-resolving promise in your interceptors. E.g.:

instance.interceptors.response.use((response) => {
  if (someCondition(response) {
    return new Promise(() => {});
  }
  return response;
});

https://stackoverflow.com/a/20068922

In short - at least in modern browsers - you don't have to worry about unresolved promises as long as you don't have external references to them

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants