0

Below is a code snippet which I came across in one of the blog for redux.

enter image description here

This snippet explains redux-thunk. But I am trying to make sense out of the weird syntax

return ({dispatch, getState}) => next => action =>{}

I tried a google search with this syntax but did not get much help. I am not understanding how next and action objects are getting their values. Can someone please help me understand what's going on here?

Mahesh
  • 1,427
  • 2
  • 19
  • 42
  • 2
    https://stackoverflow.com/questions/32782922/what-do-multiple-arrow-functions-mean-in-javascript may be this will help. – Prince Sodhi Feb 17 '20 at 14:22
  • `... => ...` is always the definition of a function which takes some parameter(s) and returns something. This just happens a few times in a row here. You have a function that returns a function that takes two parameters that returns a function that takes one parameter that returns a function that takes one parameter that returns the result of a call to `action` or perhaps `next`. – deceze Feb 17 '20 at 14:24
  • 1
    [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) – 3limin4t0r Feb 17 '20 at 14:25

1 Answers1

3

Chaining of functions in this way is usually to enable a user to bind arguments to a final function in a controlled fashion, rather than having to supply them all in one go.

In effect it is an API design choice for a piece of functionality.

({dispatch, getState}) => next => action => {}

...is equivalent to:

function({dispatch, getState}) { // destructure two properties into arguments
    return function(next) {
        return function(action) {
            // this function has access to `dispatch`, `getState`, `next` & `action`
        }
    }
}

Note that the "binding" of the arguments to the inner functions occurs through a feature of JavaScript called closures. Full explanation of closures in JavaScript here.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331