Deferred chaining - then called success callback when previous call finished with error [duplicate]

StackOverflow https://stackoverflow.com/questions/19446753

Question

I have a small dialog which should validate before close, get values from inputs and then call the callback given by dialog caller. I've realized processing inputs as function taking callback arguments, so every method in chain is asynchrone.

What I want, is to call isValid, on success callback call getValues, on success callback call okCallback. Here is my code:

var self = this
this.isValid().then(lang.hitch(this, this.getValues), function(){
  console.log('Object invalid')
  return false
}).then(function(item) {
   self.okCallback(item)
})

The problem is, that the final then() with calling okCallback is called even if isValid() finished with error, adn getValues() were not called.

Is it normal behaviour? How to make deferred chaing, so that final success callback will be called only, if everything before succeeded?

Était-ce utile?

La solution

Yes, that is normal behavior. You did already handle the error in

function(){
  console.log('Object invalid')
  return false
})

from where you did return a non-error value (false). To continue with an error, throw another one, throw the same one, or don't use the second argument to then but a separate addErrback. Or put the error handler after the .then(lang.hitch(this, this.okCallback)) so it will handle errors from getValues as well without affecting the okCallback.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top