What's the general difference between these two styles of handling .catch
blocks in Promises:
...
.catch(e => myMethod(e))
...
.catch(myMethod)
What does a Promise's .catch
pass to the received method?
e.g. Can there be additional arguments?
In
catch(e => myMethod(e))
, you are passing an anonymous function which takes a parametere
and callsmyMethod(e)
In
catch(myMethod)
, you are directly passing yourmyMethod
instead of that anonymous function (in above case), which takes a parametere
.So, both are same. And the parameter passed
e
is the "reason" for being rejected.In both cases, there is only one argument.
There's no fundamental difference between these two styles, except that an arrow function behaves differently than a real
function
, especiallythis
will beundefined
orwindow
(depending on whether strict mode is enabled or not) with afunction
, and with an arrow function it's the samethis
as the context in which it is declared.From this MDN Catch Syntax documentation:
From this MDN Arrow Function documentation: