JS redux代码的重构不会运行

时间:2017-07-08 20:18:11

标签: javascript reactjs redux react-redux es6-promise

我正在尝试做一个简单的重构,但无法让它工作 - 请帮忙。

简单地说......这有效:

gulp.task('scripts', function(cb) {
    pump([
        gulp.src(paths.scripts),
        concat('all.js'),
        gulp.dest('public/assets/js'),
        rename('all.min.js'),
        uglify(),
        gulp.dest('public/assets/js'),
        livereload()
    ], cb);
});

但是当我像这样重构时:

export function fetchPhotos(payload) {
  return dispatch => {
    dispatch(requestPhotos());

    const request = getRequest(payload, dispatch);
    fetch(request)
    .then(response => {
      if (response.status >= 200 && response.status < 300) {
        dispatch(setHeader(response.headers));
        return response.json();
      } else if (response.status == 401) {
        const error = new Error(response.statusText);
        error.response = response;
        dispatch(notAuthorized());
        throw error;
      }
    })
    .then(data => {
      dispatch(fetchPhotosSuccess(data));
    })
    .catch(err => err);
  };
}

通过在单独的函数中处理响应:

export function fetchPhotos(payload) {
  return dispatch => {
    dispatch(requestPhotos());

    const request = getRequest(payload, dispatch);
    fetch(request)
    .then(response => {
      responseHandler(response, dispatch);
    })
    .then(data => {
      dispatch(fetchPhotosSuccess(data));
    })
    .catch(err => err);
  };
}

它不起作用。似乎function responseHandler(response, dispatch) { if (response.status >= 200 && response.status < 300) { dispatch(setHeader(response.headers)); return response.json(); } else if (response.status == 401) { const error = new Error(response.statusText); error.response = response; dispatch(notAuthorized()); throw error; } } 没有返回任何数据......

0 个答案:

没有答案