在React Redux-Saga中调用api后60秒后产生收益调用超时

时间:2019-03-09 09:21:33

标签: reactjs django-rest-framework react-redux redux-saga

在我的其余API后端(django)中,我进行了大量处理,通常需要1.5分钟才能产生结果,那时候我在前端react应用程序中遇到了此错误。

export function* create(action) {
  try {
    const { payload } = action;
    const response = yield call(api.addPost, payload); **//can i set timout for this api call, here timeout after 60 second **  
    if (response.status === 200) {
      console.log('pass 200 check');
      yield put(appActions.setResourceResponse(response.data));
      console.log(response.data);
      payload.push('/add-news');
    }
  } catch (error) {
    console.log(error);
    yield put(
      a.setResponse({
        message: error.response.data,
        status: error.response.status,
      }),
    );
  }
}

错误:超时超过60000ms

因此,对等连接丢失。

如何在redux-saga中设置请求超时

1 个答案:

答案 0 :(得分:1)

如果您使用的是axios,则可以这样更改超时设置。

axios({
    method: 'post',
    url: 'http://127.0.0.1:9000',
    timeout: 60 * 2 * 1000, // Let's say you want to wait at least 2 mins
    data: {
      id: '1234',
    }
  })