0

I'm using express and am using an arrow function to handle my req,res params. I'm delegating this req,res to another helper function though.

I.e.

app.get("/Model/:id", (req, res) => { Handler.model(req, res) });    

My question is if I can avoid that redundancy and just do something like

app.get("/Model/:id", Handler.model(req, res));
2
  • 6
    Have you tried: app.get("/Model/:id", Handler.model);. Read this article to learn more about the difference of passing things by reference/value: javascripttutorial.net/javascript-pass-by-value Commented Mar 25, 2020 at 16:57
  • 1
    good but not safe, if Handler.model returns something, which not accepted by app.get( can cause issue. Better you should not always delegate. Commented Mar 25, 2020 at 17:00

1 Answer 1

1

You can probably do a η-reduction:

app.get("/Model/:id", Handler.model);

However you might have to bind it:

app.get("/Model/:id", Handler.model.bind(Handler));

Notice that unlike your original arrow function, this does pass an arbitrary amount of arguments to the model method, not exactly two, and it does return the return value of the model method instead of nothing (undefined). It depends on app.get and Handler.model whether they can deal with those minor differences.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.