4

So what do these fat-arrows do in the code below ? I could understand if they weren't two !

export default function clientMiddleware(client) {
  return ({dispatch, getState}) => {
    // ******** starts here **********
    return next => action => {
    // ******** ends here **********
      if (typeof action === 'function') {
        return action(dispatch, getState);
      }

      const { promise, types, ...rest } = action; // eslint-disable-line no-redeclare
      if (!promise) {
        return next(action);
      }

      const [REQUEST, SUCCESS, FAILURE] = types;
      next({...rest, type: REQUEST});

      const actionPromise = promise(client);
      actionPromise.then(
        (result) => next({...rest, result, type: SUCCESS}),
        (error) => next({...rest, error, type: FAILURE})
      ).catch((error)=> {
        console.error('MIDDLEWARE ERROR:', error);
        next({...rest, error, type: FAILURE});
      });

      return actionPromise;
    };
  };
}

What is the equivalent of this piece of code ?

value => value2 => {
  // some code
}
FurkanO
  • 7,100
  • 5
  • 25
  • 36

1 Answers1

4

This is basically an arrow function which returns an arrow function. You could write it more clearly as:

(value) => {
    return (value2) => {
        // some code
    };
}

The syntax is possible because the shorthand arrow function syntax without enclosing braces {...} returns the value of given in the body slot:

nils
  • 25,734
  • 5
  • 70
  • 79