Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a thunk and a closure

Tags:

javascript

I thought I knew what was a closure but I'm not so sure reading some article around react.

Is the function below a "thunk" ? (for me it's a closure, I took this from a blog article about react https://spin.atomicobject.com/2016/10/05/form-validation-react/)

Author explains: "Next, let's look at the ruleRunner function. ruleRunner is a thunk, or a function that returns a function. "

export const ruleRunner = (field, name, ...validations) => {
  return (state) => {
    for (let v  of validations) {
      let errorMessageFunc = v(state[field], state);
      if (errorMessageFunc) {
        return {[field]: errorMessageFunc(name)};
      }
    }
    return null;
  };
};

On the contrary I thought a thunk was "a function that contains all of the context (state, functions, etc) it will need in order to carry out some sort of logic in the future." from: http://www.austinstory.com/what-is-a-thunk-in-javascript/

const add = (x,y) => x + y;

const thunk = () => add(1,2);

thunk() // 3

So for me the author of the first article is wrong, he is giving a description and example of a closure not a thunk. But I may be wrong that's why I'm asking this question.

Is the first article author wrong about what is a thunk and is it right to say a thunk is a specific kind of closure "that contains all of the context (state, functions, etc) it will need in order to carry out some sort of logic in the future."

like image 700
François Richard Avatar asked May 17 '17 21:05

François Richard


2 Answers

Closures are functions that refer to independent (free) variables (variables that are used locally, but defined in an enclosing scope). In other words, these functions 'remember' the environment in which they were created.

In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created,hence enclosed to that scope. A thunk is a subroutine that is created, often automatically, to assist a call to another subroutine,therefore a thunk can be a closure, but not all thunks are closures.

Thunks are helper functions in nutshell that can be locally or globally enclosed, while a closure is only locally binded to the scope it was initialized.

like image 130
Remario Avatar answered Oct 20 '22 03:10

Remario


"ruleRunner is a thunk, or a function that returns a function."

No, that's rubbish. A function that returns a function is known as a higher-order function. The returned function is often a closure.

I thought a thunk was "a function that contains all of the context (state, functions, etc) it will need in order to carry out some sort of logic in the future."

Yes, that sounds reasonable. This is similar to a closure, however a closure usually takes some further arguments while a thunk doesn't - it only needs to be started to execute.

like image 42
Bergi Avatar answered Oct 20 '22 03:10

Bergi