promise.catch(onError);
passes a function (from the variable/constant onError
) into promise.catch
, so that it will get called if the promise is rejected.
promise.catch((err) => onError(err));
passes a function ((err) => onError(err)
) into promise.catch
, so that it will get called if the promise is rejected; that function calls onError
with the argument it receives.
They do virtually the same thing. In this specific case, they do almost exactly the same thing, because the rejection callback will never be called with fewer than or more than one argument; it's only ever called with one argument. So the only difference between the first example and the second is just the extra wrapper function.
Either way, the error object that's passed to the function comes from the promise implementation, when (if) it calls the rejection handler.
I should note that sometimes, it really matters whether you have that wrapper function on there, just not with promise callbacks. For instance, there's a big difference between:
result = someArray.map(parseInt);
and
result = someArray.map(val => parseInt(val));
The difference in that case is that map
calls its callback with three arguments, not just one. So the first ends up calling parseInt
with three arguments, but the second calls parseInt
with only one argument. Which is good, because parseInt
would try to use those subsequent arguments if they were there, in ways that would almost certainly not be what the author intended. (Just as an aside: in the case of parseInt
, specifically, you almost always want to hardcode the second argument: parseInt(val, 10)
for decimal, for instance.)