1

I saw in one tutorial this code snippet:

const myFunction = () => {
   return function (caller) {
     caller(firstFuctnion());
     caller(secondFunction());
   };
};

Could you please tell me how it works once I call it like:

myFunction()

How come that I don’t get an error when the caller argument is actually not defined?

On the other hand, if I omit it and write the code like this:

const myFunction = () => {
   return function () {
     firstFuctnion();
     secondFunction();
   };
 };

those two functions firstFuction() and secondFunction() aren’t executed. So, how exactly does that caller — or however it’s called — work?

For those of you who might want to see the whole functioning code:

const redux = require("redux");
const thunkMiddleware = require("redux-thunk").default;
const axios = require("axios");
const createStore = redux.createStore;
const applyMiddleware = redux.applyMiddleware;

const initialState = {
  loading: false,
  users: [],
  error: "",
};

const FETCH_USERS_REQUEST = "FETCH_USERS_REQUEST";
const FETCH_USERS_SUCCESS = "FETCH_USERS_SUCCESS";
const FETCH_USERS_FAILURE = "FETCH_USERS_FAILURE";

const fetchUsersRequest = () => {
  return {
    type: FETCH_USERS_REQUEST,
  };
};

const fetchUsersSuccess = (users) => {
  return {
    type: FETCH_USERS_SUCCESS,
    payload: users,
  };
};

const fetchUsersFailure = (error) => {
  return {
    type: FETCH_USERS_FAILURE,
    payload: error,
  };
};

const fetchUsers = () => {
  return function (dispatch) {
    dispatch(fetchUsersRequest());
    axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then((response) => {
        // response.data is the users
        const users = response.data.map((user) => user.id);
        dispatch(fetchUsersSuccess(users));
      })
      .catch((error) => {
        // error.message is the error message
        dispatch(fetchUsersFailure(error.message));
      });
  };
};

const reducer = (state = initialState, action) => {
  console.log(action.type);
  switch (action.type) {
    case FETCH_USERS_REQUEST:
      return {
        ...state,
        loading: true,
      };
    case FETCH_USERS_SUCCESS:
      return {
        loading: false,
        users: action.payload,
        error: "",
      };
    case FETCH_USERS_FAILURE:
      return {
        loading: false,
        users: [],
        error: action.payload,
      };
  }
};

const store = createStore(reducer, applyMiddleware(thunkMiddleware));
store.subscribe(() => {
  console.log(store.getState());
});
store.dispatch(fetchUsers());

I don't get this part. What that dispatch actually does. It is nowhere passed as an argument and it is not defined anywhere what it is. Is it function? I could write whatever there instead.

const fetchUsers = () => {
   return function (dispatch) {
     dispatch(fetchUsersRequest());
     axios
      .get("https://jsonplaceholder.typicode.com/users")
      .then((response) => {
      // response.data is the users
      const users = response.data.map((user) => user.id);
      dispatch(fetchUsersSuccess(users));
  })
  .catch((error) => {
    // error.message is the error message
    dispatch(fetchUsersFailure(error.message));
  });

}; };

Vaclav Vlcek
  • 317
  • 1
  • 6
  • 17
  • 1
    Related: [Functions that return a function](https://stackoverflow.com/q/7629891/4642212). What do you mean by “the `caller` argument is actually not defined”? That argument didn’t enter the equation yet, so to speak. `myFunction()` is a function that you need to provide the `caller` argument for, e.g. `myFunction()(alert)`. Or put differently, _“How come that I don't get an error”_ — for the same reason why `function sum(a, b){ return a + b; }; sum;` doesn’t throw an error despite `a` and `b` not being provided; the function that needs these parameters hasn’t been called yet. – Sebastian Simon Apr 21 '21 at 17:54
  • You need to use `myFunction()()` to reach the inner function – Sagar V Apr 21 '21 at 17:55
  • 1
    You've already got some good answers so I'm not going to write an essay. In the case of redux-thunk, the thunk middleware allows you to `dispatch` an action which is a function of `dispatch` instead of just a plain action object. The inner function is called by the thunk middleware when the function is dispatched to the store. – Linda Paiste Apr 22 '21 at 01:44
  • Thank you @LindaPaiste, this is the part what I was also missing. Due to redux-thunk, `dispatch` accepts function. – Vaclav Vlcek Apr 22 '21 at 07:03
  • you are not calling your function, that's all. – webduvet Apr 23 '21 at 14:49

3 Answers3

5

Let’s consider a simplified version of this code. Before you added some important context to your question (the fact that myFunction() is actually being used, and how it’s used), I wasn’t quite sure where your confusion was, so this answer starts with a general explanation of callbacks, higher-order functions, and currying.

First, let’s turn the function into an arrow function, so the code is more straight-forward to read. There are differences between them, but let’s ignore them for now. Let’s also rename myFunction to outer, so we know which function we’re talking about.

const outer = () => {
   return (caller) => {
     caller(something);
   };
};

outer is a function. outer() is also a function; that means, the return value of the function outer is, itself, a function.

Since outer is a function that returns a function, this makes outer a higher-order function (HOF) (according to one of the two definitions of HOFs)1.

To be explicit, you can reference the returned function by using another variable:

const inner = outer();

How come that I don’t get an error when the caller argument is actually not defined?

That’s a strange question to ask; when outer() is called, it doesn’t touch on caller yet. Only calling inner does.

Ask yourself this: why do you not get an error for a and b not being defined here?2

const sum = (a, b) => a + b;

sum;

That’s right: sum isn’t even called! Just like outer() isn’t even called.

Even one step further, this won’t throw an error even if somethingUndefined isn’t defined anywhere and cannot be set anywhere. (But you will get linter warnings.)

const someFunction = () => somethingUndefined;

someFunction;

Why do you not get an error here, in this common example of HOFs, despite b not being defined at the point where add(5) is assigned to addFive?

const add = (a) => {
  return (b) => a + b;
};
// Or more simply:
// const add = (a) => (b) => a + b;

const addFive = add(5);

console.log(addFive(3)); // Pints 8.

Because the function that needs b, i.e. addFive, has not been called yet.

If you want to force an error due to caller not being defined, you’d have to call the function that actually needs caller defined. The function in question is outer()! You call it, like any other function, with (): outer()(). Now you get a TypeError because caller isn’t a function.

This “double call” is called currying.


The code can also be rewritten like this. The only difference is that inner is now also accessible in the outer scope where outer is. See What is the scope of variables in JavaScript?.

const inner = (caller) => {
  caller(something);
};
const outer = () => inner;

inner takes the role of the returned function (outer()). It takes a function as an argument and calls it with something as an argument.

outer is just a function that returns the function inner and nothing else. It does not call it, it doesn’t touch on caller, it has nothing to do with something.

outer() and inner are identical.


On the other hand, if I omit it and write the code [without caller], those two functions firstFuction() and secondFunction() aren’t executed. So, how exactly does that caller — or however it’s called — work?

First, let’s fix the semantics: firstFuction() and secondFunction() are function calls. Whether they are functions or not, depends on what firstFuction and secondFunction return. firstFuction and secondFunction are functions (at least the code implies that they are).

More specifically, consider alert:

  • alert is a function; you can call it like alert().
  • alert() is not a function; you cannot call it like alert()().

caller is a callback function. If you omit caller from the code, then myFunction()() would be the proper call that doesn’t throw an error and simply calls firstFuction and secondFunction.

But the point of the callback is to pass the results of firstFuction and secondFunction to the function caller. You have to provide that function yourself, e.g. the alert function.

myFunction()(alert) calls the inner function, passing alert to it. The inner function calls firstFuction and secondFunction and passes their results to the caller, which we specified to be alert.


Could you please tell me how it works once I call it like myFunction()?

Quite simply, nothing happens here. myFunction returns a function, and that’s it; that function isn’t used further, so it is discarded.

There is a key aspect of higher-order functions that isn’t demonstrated in this code: encapsulating values. Consider this code:

const add = (a) => {
  let count = 0;

  return (b) => {
    ++count;
    console.log(`The function to add ${a} to something else has been called ${count} time(s).`);

    return a + b
  };
};

add and b work similarly to myFunction and caller from before, but now, the outer function also needs an argument, so add() won’t help. add(5) calls the add function and sets argument a to 5. Further, add(5)(3) calls the inner, returned function, sets b to 3 and returns 8.

But there’s also some state inside add in the form of a variable called count.

const addFive = add(5);

console.log(addFive(3)); // Prints 8.
// But also logs: "The function to add 5 to something else has been called 1 time(s)."

console.log(addFive(10)); // Prints 15.
// But also logs: "The function to add 5 to something else has been called 2 time(s)."

count is only accessible inside outer (including any function inside of it); it is “encapsulated”. This is a common way to hide some state that should only be accessible inside a specific function.

a is actually also encapsulated inside add(); a and count have the same scope and aren’t that different from each other, just that a can be set as an argument when calling add whereas count cannot.


You’ve then added more context, where the call looks more like this:

someOtherFunction(myFunction())

This is different from the situation before where you just wrote the call like myFunction(). Now, the result is not discarded, as it is fed into someOtherFunction (in the original code: store.dispatch).

[caller] is nowhere passed as an argument and it is not defined anywhere.

But now it is the job of someOtherFunction to call myFunction() with the appropriate callback function as an argument. Now, you don’t pass the function yourself; instead, someOtherFunction does it for you.

Compare this to Where do the parameters in a JavaScript callback function come from?. If you go back to the sum example function from earlier, then either you pass the arguments yourself:

sum(1, 2);

or some library or framework does it for you:

// Library code
const doTheRightThing = (callback) => callback(1, 2);
// Your code
doTheRightThing(sum);

or a built-in (or host-defined) function does it for you:

[ 1, 2, 3 ].reduce(sum, 0);

It doesn’t matter where these parameters come from or where the function is called from. But it is called, and it is called with the right arguments somewhere.


1: The two definitions of a higher-order function are

  1. a function that returns a function, or
  2. a function that takes another function as an argument.

outer fits the first definition. outer() (independently and coincidentally) fits the second definition.

2: Okay, granted, sum() would also not throw an error, but a and b would, nonetheless, be undefined, each. undefined + undefined has semantics that don’t result in an error, but hopefully you get the point: in your original code, myFunction()() would throw an error only because undefined(firstFunction()) isn’t possible. The culprit is the same: caller is undefined.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • Thank you Sebastian for the extensive explanation. I went through you point and I am getting you point. E.g. regarding you explanation "But the point of the callback is to pass the results of firstFuction and secondFunction to the function caller. You have to provide that function yourself, e.g. the alert function.)" The thing is, that nowhere is defined what that caller does. I added the whole code, what I don't understand is that `dispatch` parameter that just doesn't make sense to me... – Vaclav Vlcek Apr 21 '21 at 20:28
  • @VaclavVlcek Read the edited answer (after _“You’ve then added more context \[…\]”_). – Sebastian Simon Apr 21 '21 at 20:50
4

Functions are treated like any other values, meaning they can be returned from functions. Calling myFunction will simply return the inner function, which will require a parameter caller to be passed to it when it itself is called.

The inner function will remember any arguments that were passed to it's enclosing function.

An example I use to understand this concept is that of a "multiplier factory" function

const answer = function() {
    console.log("The answer is: ");
}

const multiplier = function(factor) {
    return function(n){
        answer();
        console.log( n * factor);
    };
}

const twice = multiplier(2);
const triple = multiplier(3);

twice(4); // The answer is: 8
triple(5); // The answer is: 15

In the code above twice and triple are essentially the following

const twice = function(n) {
    console.log("The answer is: ");
    console.log(n * 2);
}

const triple = function(n) {
    console.log("The answer is: ");
    console.log(n * 3);
}

twice(4); // The answer is: 8
triple(5); // The answer is: 15

To understand more about these concepts, I suggest reading up on "Higher Order Functions" and "Closures" in Javascript

Ruban Prakash
  • 83
  • 1
  • 7
  • Thank you Ruban for your explanation. Just one more question please: `const twice = multiplier(2)` from you first snippet. Is that number `2` `factor` or `n`? Or both :) I am still not getting that you expects two parameters: `factor` and `n`, but you are passing just one number `2` finally. Thank you and have a nice day! – Vaclav Vlcek Apr 22 '21 at 07:13
  • 1
    @VaclavVlcek `multiplier` is a function that accepts `factor` as a parameter and returns a function. So, in `multiplier(2)`, `factor` becomes `2`. At this point you don’t need to think about the inner function. Mentally, it’s just `const multiplier = function(factor){ return` … something … `; };`. None of the functions accept two parameters, so you don’t pass two arguments. – Sebastian Simon Apr 22 '21 at 07:20
0

I think you haven't printed the whole snipet. because, there is no definition to firstFunction() and secondFunction() inside myFunction().

to answer your question, if you want to call the function defined in myFunction(), you need to assign the value to a new variable as it is returning the function. then simple make the function call.

const myFunctionResult = myFunction();
myFunctionResult(someOtherCallerFunction);

but note this will throw an error for the reason mentioned above.

Mike Araya
  • 155
  • 1
  • 11
  • _“you need to assign the value to a new variable”_ — No, you don’t _need_ to. _“but note this will throw an error for the reason mentioned above.”_ — The OP is already confused how the code works; why not show them how to do the call properly? – Sebastian Simon Apr 21 '21 at 18:06
  • you are right you can resort to chaining, but from the looks of it that might confuse them, that's why I resorted to the long solution. there is not goal based on the question they asked, so I just answered them what is blocking them. it the question was clear you are right we can explain further. – Mike Araya Apr 21 '21 at 18:10