首页 > 解决方案 > 等到 take 完成的操作不起作用(redux saga)

问题描述

我正在使用 redux-saga 和 redux 来处理 react 的异步过程,但是 put&take 不起作用。知道为什么吗?

在 saga 的生成器函数中,我test-action通过put. 然后我想等到动作完成,所以我尝试通过 来做take,但take没有被调用。

在 redux-devtool 中,我可以发现test-action肯定是派遣的。

function* firstActionSaga(){
  // dispatch secondAction
  yield put(secondAction())

  // If secondAction finished, the below line should be called.
  yield take("SECOND_ACTION")

  console.log("second action finished.")
}

function* rootSaga() {
 yield all([

   // Wait for firstAction, and start firstActionSaga
   yield takeEvery(FIRST_ACTION, firstActionSaga)
])
}

预期的

  1. 等待 firstAction,然后启动 firstActionSaga
  2. 调度 secondAction 由put
  3. 等到 secondAction,然后做点什么。

实际的

  1. 等待 firstAction,然后启动 firstActionSaga
  2. 调度 secondAction 由put
  3. take没有被解雇

标签: javascriptreactjsreduxredux-saga

解决方案


最后,我可以使用“putResolve”来做我想做的事。


推荐阅读