3

Q: Does JS allow you to write functions to return functions?
A: Yes it does.

Doesn't mean you should do this. What's wrong with you?

Comments
  • 6
    And what’s wrong with that?
  • 3
    Well just don't call them in a chain if you want more context..

    const filter = respondWithFilteredResult(res,options);

    return filter(interactions);
  • 2
    can be used well in a delegate-type situation
  • 0
    I’ve seen it used. It looks "smart" but it’s confusing
  • 3
    This is also termed higher-order function and is one of the most useful low-level patterns you can use in JS. Express middleware and metalsmith plugins are basically that.

    If the func has jsdoc comments or TS definitions and you use an editor which shows hints it is super-easy to use and super versatile.
  • 2
    It's commonly known as a factory
  • 0
    That doesn’t seem that terrible to me.
  • 0
    Yeah this would just not be readable.

    Either to use some function composition or to restructure like:

    const runFilter = createResponseFilter(response, options);

    return runFilter(interactions);
  • 0
    @webketje yeah, when you're writing middleware.

    But this is literally a controller. Supposed to be easy to see what it does, easy to change with new business needs. What the fuck is happening there is anybody's guess.
Add Comment