0

I have the following code:

fooService.update(this.bar).then( this.$emit('updated', this.updatedBar),).catch(err => {...

If an error is encountered, then the error is not caught. If I change the code to be:

fooService.update(this.bar).then(x => {this.$emit('updated', this.updatedBar);}).catch(err => {...

Then the error is caught and shows as expected. Can anyone explain to me what is going on and why it behaves in that way?

Edit

Underlying service code:

function updateBar(bar) {
  return $http.put(`/api/bar/${bar.Id}`, bar);
}
11
  • 2
    Why do you combine await syntax with Promise syntax? Commented Nov 14, 2019 at 14:03
  • @Cristy oddly enough, I know someone that does the same, it does end up ignoring the await in the sense it will do the .then, although I assume it doesn't fully ignore it. Commented Nov 14, 2019 at 14:07
  • To hazard a guess, I'd say because in the first example the .then is not being passed a function Commented Nov 14, 2019 at 14:08
  • @Cristy - amended the code, the same happens however it is approached. Commented Nov 14, 2019 at 14:08
  • 1
    @Leonidas199x Thanks, I can't get it to replicate still but you should be doing it the second way anyway Commented Nov 14, 2019 at 14:27

1 Answer 1

1

So I still think the error is happening in the this.$emit the reason why, in

fooService.update(this.bar).then( this.$emit('updated', this.updatedBar),).catch(err => {

It has to evaluate the this.$emit first as you're setting the response from that function as the .then and not the call itself.

Proof of it doing that

function emit(){
  console.log('emit')
}

var promise = new Promise(function(resolve,reject){
setTimeout(() => {
  console.log('promise is done')
  reject();
}, 1000)
})

promise.then(emit()).catch( function() {console.log('carry on');})

notice how it logs "emit" first

Now if that errors you can see it doesn't hit the catch

function emit(){
  console.log('emit')
  throw new Error("bad")
}

var promise = new Promise(function(resolve,reject){
setTimeout(() => {
  console.log('promise is done')
  reject();
}, 1000)
})

promise.then(emit()).catch( function() {console.log('carry on');})

So under the hood it's doing this (the simplest way I can think of)

emit()

try{
    getService()
} catch{
    ...
}

Whereas if you actually pass the .then a function it changes the order of things

function emit(){
  console.log('emit')
  throw new Error("bad")
}

var promise = new Promise(function(resolve,reject){
setTimeout(() => {
  console.log('promise is done')
  reject();
}, 1000)
})

promise.then(() => {emit()}).catch( function() {console.log('carry on');})

and again under the hood it looks like this

try{
    getService()
    emit()
} catch{
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for taking the time to do this, really appreciate it. That pretty much answers my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.