两个胖箭互相跟随

时间:2016-05-19 12:30:53

标签: javascript syntax ecmascript-6

那么这些胖箭在下面的代码中做了什么?我能理解他们是不是两个人!

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;
    };
  };
}

这段代码的等价物是什么?

value => value2 => {
  // some code
}

1 个答案:

答案 0 :(得分:3)

这基本上是一个返回箭头功能的箭头功能。你可以写得更清楚:

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

语法是可行的,因为the shorthand arrow function syntax without enclosing braces {...} returns the value of given in the body slot