【问题标题】:Can we use race with takeEvery ?我们可以使用 match 和 takeEvery 吗?
【发布时间】:2017-05-24 00:47:06
【问题描述】:

我正在使用 saga,并尝试在 takeEvery 和超时之间进行比赛。如果用户没有执行操作,我想调度 DISMISS 操作(超时)。但是如果用户已经完成了操作,我不想发送 DISMISS 操作。

我没有成功,所以我的问题是,我可以使用 takeEvery 进行比赛吗?

const id = 5 // just for the example
const {a, b} = yield race({
  a: yield takeEvery(DISMISS, function* ({payload}) {
    if (payload === id) {
      // find a way to win the race
    }
    // continue to listen
  }),
  b: yield call(delay, 6000),
})

【问题讨论】:

    标签: javascript reactjs redux-saga


    【解决方案1】:

    我找到了不应该使用 takeEvery 而是使用正常调用一段时间的解决方案:

    答案:

    const {timeout} = yield race({
      _: call(yourFunction$, param),
      timeout: call(delay, alertDelay),
    })
    

    与:

    function* yourFunction$(param) {
      let yourCond = true
      while (yourCond) {
       const action = yield take(YOUR_ACTION_TYPE)
       // do what you want 
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多