【问题标题】:Redux Saga takeLatest doesn't cancel previous multiple requestRedux Saga takeLatest 不会取消先前的多个请求
【发布时间】:2021-01-10 22:19:40
【问题描述】:

今天刚学redux-saga,我读到的是takelatest会取消同一个请求,只执行最新的请求,但我的代码似乎没有这样做,每次我点击获取按钮时,它都会附加一个新请求而没有取消上一个,希望有人能解释一下原因,谢谢。

【问题讨论】:

    标签: reactjs redux redux-saga


    【解决方案1】:

    它将取消 saga,但这只是意味着它不会恢复 saga(除非进入 finally 块,如果有的话)。如果还有其他需要拆解的事情,例如取消 axios 网络请求,那么您需要自己添加代码。例如:

    function* getPeople() {
      const cancelTokenSource = axios.CancelToken.source();
      try {
        const { data } = yield axios.request({
          method: 'get',
          url: 'http://localhost:3004/people',
          cancelToken: cancelTokenSource.token
        });
        yield put(actions.setPeople(data));
      } catch (e) {
        yield put(actions.setPeopleError({
          type: 'get_data',
          message: e.message
        });
      } finally {
        if (yield cancelled()) {
          cancelTokenSource.cancel();
        }
      }
    }
    

    还有另一种选择。当你向 redux-saga 产生一个 Promise 时,如果该 Promise 上有一个特殊的属性,那么 redux-saga 会在 saga 被取消时自动调用该属性。因此,如果您发现自己经常取消 axios 网络请求,您可以设置一个辅助方法,将该属性添加到您的 axios 承诺中:

    import { CANCEL } from 'redux-saga';
    
    const req = () => {
      const cancelTokenSource = axios.CancelToken.source();
      const promise = axios.request({
        method: 'get',
        url: 'http://localhost:3004/people',
        cancelToken: cancelTokenSource.token
      });
      promise[CANCEL] = () => cancelTokenSource.cancel();
      return promise;
    }
    
    function* getPeople() {
      try {
        const { data } = yield call(req);
        yield put(actions.setPeople(data));
      } catch (e) {
        yield put(actions.setPeopleError({
          type: 'get_data',
          message: e.message
        });
      }
    }
    

    【讨论】:

    • 谢谢,第一个例子成功了,我无法让第二个例子工作我不知道原因,没有错误。
    • 感谢 Nicholas 提供的工作示例。这正是我一直在寻找的。有没有一种方法,当已经有一个正在运行的请求时,多个 axios 请求不会首先发送?只是想避免任何取消。谢谢。
    • @Mahesh 对于这种行为,我通常会使用takeLeading 而不是takeLatest。 takeLeading 监听一个动作,然后它会忽略所有其他动作,直到它完成第一个动作。
    猜你喜欢
    • 2019-06-05
    • 2012-10-08
    • 2020-12-29
    • 2018-06-29
    • 2018-10-09
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 2022-01-26
    相关资源
    最近更新 更多