5

How can I read the following arrow function in English? I can understand that there are 2 parameters namely dispatch and getState. How do I read the remaining part?

How to write it as a normal function?

    const apiMiddleware = ({ dispatch, getState}) => next => action => {  
  • 6
    There's actually only one parameter in every function there - the `apiMiddleware` function accepts *one* parameter, an object with two properties, `dispatch` and `getState`. It then returns a function that takes in a `next` parameter and returns *another* function, which takes an *action* parameter. – CertainPerformance Dec 12 '18 at 07:30
  • Depends on how it is constructed. I found that usually it reads "goes to", but you can check also here: https://stackoverflow.com/questions/274022/how-do-i-pronounce-as-used-in-lambda-expressions-in-net – Watth Jan 31 '21 at 20:46

6 Answers6

1

Written as a normal function, the declaration will look like this:

const apiMiddleware = function({ dispatch, getState}){
 return function(next) {
    return function(action) {
      return something;
  }
 }
} 

I suggest you to read this great article about higher order arrow functions here

Stundji
  • 855
  • 7
  • 17
0

I hope you find this response useful.

let squarevalue = input => { input * input }

let multiplyvalue = (input, multiplier) => { input * multiplier }
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
Paul Tofunmi
  • 435
  • 4
  • 15
0

This is what it might look like if it were written in es5 syntax

var apiMiddleware = function apiMiddleware(_ref) {
  var dispatch = _ref.dispatch;
  var getState = _ref.getState;
  return function (next) {
    return function (action) {
      console.log('action function body');
    };
  };
};
ksav
  • 20,015
  • 6
  • 46
  • 66
0

Writing this:

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

As a normal function (by normal I mean no arrows):

const apiMiddleware = function({dispatch, getState}) {
    return function(next) {
        return function(action) {
            //Rest of the code
        }
    }
}

And here's how you'd write it in ES5:

var apiMiddleware = function(data) {
    var dispatch = data.dispatch;
    var getState = data.getState;
    return function(next) {
        return function(action) {
            //Rest of the code
        }
    }
}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

That's one of the ES6 nice features called object destructuring. This one is also interesting and may be related to your code in question:

Try pasting the code into this and see the transformed script as vanilla JS: https://es6console.com/

Yom T.
  • 8,760
  • 2
  • 32
  • 49
0

you can use the following babel compiler to convert the es6 code into es5 -

babel compiler

onkar.v
  • 135
  • 9