【问题标题】:redux-saga: tracking multiple async tasksredux-saga:跟踪多个异步任务
【发布时间】: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)
}
  1. componentA 调度 doAsync1
  2. componentB 调度 doAsync1

  3. 组件 C 调度 doAsync2(为了更好的衡量标准)

  4. componentA 调度 doAsync1

  5. 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


【解决方案1】:

function* generator(){
    yield call(api,params);
    yield call(api2, params2);
}

const gen = generator;

gen.next() // done: false/true
gen.next() // done: false/true

【讨论】:

猜你喜欢
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多