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

Both then and catch executing in same call #199

Closed
dmt0 opened this issue Jan 20, 2016 · 15 comments
Closed

Both then and catch executing in same call #199

dmt0 opened this issue Jan 20, 2016 · 15 comments

Comments

@dmt0
Copy link

dmt0 commented Jan 20, 2016

I'm doing this call:

axios.get('/groups', {
  baseURL: 'http://localhost:5000',
  headers: { Authorization: 'Bearer ' + getAccessToken(),  'Content-Type': 'application/json; charset=utf-8' }
})
  .then((response) => {
    console.log('then response',response)
    success(response.data)} )
  .catch((response) => {
    console.log('catch response',response)
    error(response.status, response.data.description)})

I'm calling this exactly once. I get a 200 OK response, then block executes, with response being an object I would normally expect. Right after that catch executes, response being a string:

TypeError: Cannot read property 'groups' of null(…)

Closing: my bad, it was an error somewhere very far in the code, still trying to figure out how it made it into the promise...

@nilaybrahmbhatt
Copy link

I have the same error. Both then and catch executing in same call

@shin-monkey
Copy link

I am also getting this error. Is there an explanation for this?

@dmt0
Copy link
Author

dmt0 commented Jan 9, 2017

Basically it has to do with how promises works:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw

The throw statement throws a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate.

@tomasswood
Copy link

I had the same issue and mine was to do with having .catch before .then. Whoops.

@greenzeal
Copy link

tomasswood this should not be the case actually.
const doRefreshToken = () => {
return new Promise(
function (resolve, reject){
if (success){
resolve(success)
}else{
reject(error)
}
})
}

so actually there is no any difference if write .catch first or .then

@dengunya
Copy link

@greenzeal there is difference. this is strange, but the order does matter.
I wanted to use smth like
var func = () => axios.post(....).catch();
to implement error handling before calling this async function.
func().then(...)
But in this case "then" callback fires on error (ex. 404 status) and "catch" cb is ignored.

@greenzeal
Copy link

Yes, I was mistaken, there is a order in promises. First to use .then after .catch

@dacastro4
Copy link

dacastro4 commented Jan 26, 2018

This just happens to me and my problem was (correct me if I'm wrong) that any kind of error you have inside the then() callback it will be caught by the "catch" callback.

Example:

axios.get(url)
    .then( data => {
        //some sort of error
    })
    .catch(error => {
        /it's gonna fall here. If you console log "error" is gonna be the error that happened in the then callback 
    })

@Jam0r85
Copy link

Jam0r85 commented Feb 21, 2018

@dacastro4 THANK YOU! Was pulling my hair out trying to figure out why my request kept catching when it was successful. Re-read through my .then and found a bug 👍

@sachintbits
Copy link

@dacastro4 Thank you, my error was in the then execution. Fixed the error in the function that was called by the then statement and the problem was resolved.

@LuckyLam34
Copy link

LuckyLam34 commented Mar 21, 2019

@dacastro4 Thank you so much, you're the man!

@zmechanic
Copy link

zmechanic commented Mar 23, 2019

My problem was that I didn't need result in then, it was highlighted as unused, so in a hurry I removed it to and got the following:

    this.processDropFromPalette(data.component, data.dataTransfer.items)
        .then(this.setState(state => {
            data.component.isChanging = 1;
            return { connectedComponents: state.connectedComponents };
        }))
        .catch(reason => {
            this.ctx.publish(new Message.ShowErrorMessage(reason));
        });

And instead of being a lambda func declaration in .then it become a direct function call, so this.setState() was called as soon as this.processDropFromPalette returned, without waiting for resolve().

@mohit61
Copy link

mohit61 commented Jul 4, 2019

thanks.

@Tony-Ndichu
Copy link

Tony-Ndichu commented Aug 20, 2019

This just happens to me and my problem was (correct me if I'm wrong) that any kind of error you have inside the then() callback it will be caught by the "catch" callback.

Example:

axios.get(url)
    .then( data => {
        //some sort of error
    })
    .catch(error => {
        /it's gonna fall here. If you console log "error" is gonna be the error that happened in the then callback 
    })

I see several people have given a thumb's up to @dacastro4 's comment, could anyone please care to explain how his comment helped you guys with your specific situations? Am looking at the answer but I don't understand how to use it to prevent my .then and .catch both from executing at the same time.

@gabrieljcandia
Copy link

This just happens to me and my problem was (correct me if I'm wrong) that any kind of error you have inside the then() callback it will be caught by the "catch" callback.
Example:

axios.get(url)
    .then( data => {
        //some sort of error
    })
    .catch(error => {
        /it's gonna fall here. If you console log "error" is gonna be the error that happened in the then callback 
    })

I see several people have given a thumb's up to @dacastro4 's comment, could anyone please care to explain how his comment helped you guys with your specific situations? Am looking at the answer but I don't understand how to use it to prevent my .then and .catch both from executing at the same time.

I think it means that what could be happening, is that maybe there's an error in the "then" block, for what the "catch" block is being excecuted, and that being the reason why both blocks get excecuted. If that is true, doing console.log(error) in the "catch" block should show details. In my case that helped solve the problem.

@axios axios locked and limited conversation to collaborators May 21, 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