0

Background

According to the MDN docs, Promise.prototype.catch() takes a parameter called onRejected, which is a function that is called when the Promise is rejected. (Internally, this calls Promise.prototype.then(undefined, onRejected).)

According to the docs, onRejected “has” one parameter, reason, which is the rejection reason. In the examples given, this parameter is always present.

E.g.,

p.catch(function(e) {
  console.error(e);
})

Question

I am writing code where I do not want to use the reason parameter, so something like this:

p.catch(function(e) {
  foo();
})

Is it better practice to leave in the e parameter, since according to the docs onRejected is supposed to have it? (ESLint will also complain about this).

Or is it common practice to remove it? As in

p.catch(function() {
  foo();
})

PS: I know about optional catch binding, but that is for try-catch blocks.

hb20007
  • 421
  • 10
  • 21
  • 4
    If you don't use the parameter, then it really doesn't matter whether you include it or not. – VLAZ Oct 19 '21 at 12:03
  • @VLAZ The MDN documentation usually marks "optional" parameters as such, but this one is not. So I was wondering if there is any reason to keep it. – hb20007 Oct 19 '21 at 12:07
  • 1
    It's *you* who supplies the function that will be called. And it will always be called with one parameter. However, you can use as many or as few as the arguments that will be passed in. Creating `foo = () => 4` and calling it as `foo(1, 2, 3)` is valid. The arguments are just ignored. – VLAZ Oct 19 '21 at 12:10
  • Writing `p.catch(foo)` is more direct. TypeScript might complain, but I don't write TypeScript. – traktor Oct 19 '21 at 12:37

0 Answers0