2

Wanted to see if this was possible and if so how. On Angular's .catch block, I want to have my handleError callback and pass an id. but regardless the function gets called, I know it because of how the promise is formed. but I was hoping there was a way to still pass in a parameter without calling the function beforehand.

//controller
function handleError(id) {
  showErrorMessage(id);
}

callFactory
  .then(update)
  .catch(handleError)
  .finally(clearLoader);

// would like to use something like this.

callFactory
  .then(update)
  .catch(handleError(2)) //this gets called regardless
  .finally(clearLoader);
2
  • Use a function factory that returns a custom version of handleError that catch can call.
    – zero298
    Commented Oct 19, 2017 at 15:40
  • yeah, that's what I currently have. just curious if the above was possible. Commented Oct 19, 2017 at 15:41

1 Answer 1

4

The first argument of the .catch is always going to be the exception that was thrown. You could probably achieve what you're looking for with a closure method. Something like:

function handleError(id) {
  return function(err) {
    // access to both err and id
}

Altertnatively, you could throw the id in the .catch, then it would be picked up by the next catch

.catch(err=> { throw id })
.catch(id=> ... )
1
  • Closure FTW! Thanks, Austin. Commented Oct 19, 2017 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.