【发布时间】:2016-12-13 02:58:48
【问题描述】:
我正在使用 sagas 跟踪多个异步任务,但有一个问题我无法完全解决:
function* performTask1() {
// Some logic here to takeLatest for the relevant component
// check if component id matches?
// Only perform API call with the latest
const { result } = yield takeLatest('doAsync2')
}
function* performTask2() {
const { result } = yield call(api, args)
// do something with results (not relevant)
}
function* watchAsyncTasks() {
yield takeEvery('doAsync2', performTask2)
yield takeEvery('doAsync1', performTask1)
}
- componentA 调度 doAsync1
componentB 调度 doAsync1
组件 C 调度 doAsync2(为了更好的衡量标准)
componentA 调度 doAsync1
- componentB 调度 doAsync1
如何使用 sagas 确保只有 sagas 3、4 和 5 完成其 API 调用?
【问题讨论】:
-
complete their API call是什么意思?您可以通过sagas的调度成功操作来检查这一点 -
完成一个api调用意味着从服务器返回数据。我没有说清楚的问题是我们不希望动作 1 的结果在动作 4 之前到达(我们不能保证仅仅因为 1 在 4 之前触发)。我们要么想按顺序处理 1 和 4,要么确保我们只取 4 的结果
-
也许只用 takeLatest 而不是 takeEvery?
标签: redux react-redux redux-saga saga