Difference between a thunk and a closure

Viewed 2212

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."

2 Answers
Related