0

I'm trying to do this:

var error = (reason) => {
        console.log(reason);
    };

//actually other promise
Promise.resolve()
    .catch(error())
    .then(render(req, res));

How can I pass the reason to the error function?

Himmators
  • 14,278
  • 36
  • 132
  • 223
  • 4
    `.catch(error())` ==> `.catch(error)` – Tushar Jun 29 '16 at 09:34
  • @Tushar was even faster than your question. that's impressive, Tushar :D . Anyway, further reading about referencing a function in js: http://stackoverflow.com/questions/373157/how-can-i-pass-a-reference-to-a-function-with-parameters – briosheje Jun 29 '16 at 09:37
  • 2
    @Tushar has been waiting for this question to be asked since early 2015.. – cviejo Jun 29 '16 at 09:38

2 Answers2

2

You have to use catch(error) instead of catch(error()).

The catch method expects a Function as argument. To actually add the named function itself, just type error. If you type error(), the method will be executed first. As a result, it will log "undefined" and, as the function does not return anything, is evaluated to "undefined" as well. So what you essentially call is catch(undefined).

str
  • 42,689
  • 17
  • 109
  • 127
1

Use catch(error) instead of catch(error()) to provide the function defined in the var error

M. Junaid Salaat
  • 3,765
  • 1
  • 23
  • 25