【问题标题】:two fat-arrows following each other [duplicate]两个彼此跟随的胖箭头[重复]
【发布时间】:2016-09-16 08:06:42
【问题描述】:

那么这些粗箭头在下面的代码中做了什么?如果他们不是两个,我可以理解!

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
}

【问题讨论】:

    标签: javascript syntax ecmascript-6


    【解决方案1】:

    这基本上是一个返回箭头函数的箭头函数。你可以更清楚地写成:

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

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

    【讨论】:

    • 我的错,谢谢你提醒我:)
    猜你喜欢
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2013-03-16
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多