6

The title of this question is probably misleading. I'm having some trouble wrapping my head around this concept. I want to pass all of the arguments into a function, but call it with those arguments later.

I'm currently doing something like this:

function copyRecords(userId, options) {
    const getRecordsWrapper = (userId, options) => () => getRecords(userId, options);
    abstractionService = new MyAbstractionService(getRecordsWrapper);
    ...etc
}

This is where I would save and later call the function:

class MyAbstractionService {
    constructor(getRecords) {
        this.getRecords = getRecords;
    }
    performSomeAction() {
        return this.getRecords();
    }
}

I need to do this because getRecords takes different arguments in different areas, and I want to abstract them into one service without having to pass a bunch of arguments into the service. That will get messy, as I need to do this for some other functions as well.

Am I thinking about this correctly? Is there another way to do this besides having a function return another function which returns my function? Also, not sure if this is important but getRecords returns a Promise. I don't want to start working on the Promise until I'm in the abstraction service later though.

1
  • 2
    In short, there is nothing wrong with what you're doing. You've correctly intuited that by using a higher order function, you can avoid repetitiously passing the same arguments every time you call getRecords. Commented Jun 30, 2018 at 21:53

3 Answers 3

4

In functional programming terms, what you're describing is Currying, and in JavaScript you can achieve the simplest idea of it using the bind() function:

const getRecordsWrapper = getRecords.bind(null, userId, options);

The first argument passed to bind is the function context (the this) of the to-be-called-later function, while the rest of the arguments are passed as regular arguments.

If you're looking for a fully-featured functional programming framework in JavaScript, take a look at Ramda.js (for example).

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

2 Comments

This isn't strictly "currying" but rather an example of a higher order function.
Thanks for the insight, I almost tagged the question as currying... didn't pay enough attention in my paradigms course apparently. bind looks like exactly what I need.
1

Using bind() is a good idea. Here's an alternative. Leaving it up to readers which is preferred.

function copyRecords(userId, options) {
  return () => {
    return getRecords(userId, options);
  }
};

Note that this is pretty much identical to:

function copyRecords(userId, options) {
  return getRecords.bind(this, userId, options);
};

So it comes down to a subjective preference.

Comments

1

As partial application is a common problem, there is a proposal to support that directly in javascript without .bind. If the proposal gets through, it will look like:

new MyAbstractionService(getRecords(userId, options, ...))

1 Comment

Oh god I need this proposal to happen.

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.