我是否应该始终对 return 承诺的函数使用 redux-saga `call` 效果?
Should I always use redux-saga `call` effect for functions that return promise?
假设我有一个函数
function* request(url) {
return global.fetch(url).then(response =>
_.result(response, 'json'))
}
两个代码示例对我来说都很好
const {data} = yield call(request, 'http://example.com/api');
yield put(actionSuccess(data));
和
const {data} = yield request('http://example.com/api');
yield put(actionSuccess(data));
故题。对 return 承诺的函数使用 call
效果有什么好处?
优点是可以看到应用程序中正在发生的事情。
您想知道状态更新的时间、原因和方式 [1].
call
[2] triggers an effect when a saga monitor is configured on the saga middleware before running the effect and after the effect is resolved.
在配置了 saga 监控器且未使用 call
效果的情况下,我在监控器日志中看到了这一点。
然而call
效果,
使用 call()
的一些好处:
- Testability, the only benefit the docs seem to mention (lower half of the page). I recommend redux-saga-test-plan,它允许通过直接提供 return 值来模拟
call()
和其他效果。
- 更细粒度task cancellation. Sagas are ES6 generators, which only yield control (back to redux-saga middleware) when you use the
yield
statement. These are the only points where cancellation can happen. A yield call()
gives redux-saga an opportunity to cancel the task right before it's about to make that no longer needed call. Task cancellation is performed using gen.return()
generator method. [1] [2]
- 总体上更细粒度的任务调度。 Redux-saga 的并发模型基本上是协作式多任务处理。而 JavaScript 是单线程的。 A 因此,当你
yield
时,这是 redux-saga 中间件执行任何类型的其他任务调度的唯一机会。 (不过,我不确定 redux-saga 在这方面有多好。)
有关更多信息,我认为最好在 redux-saga 的 Github 上开一个问题直接询问维护者。
更新: 由于这个答案越来越受到关注:使用 redux-saga 前请三思。 它提供:
- 现在已过时的基于生成器的
async/await
版本自 ES7 可用
- CSP (频道) Golang用户应该熟悉
- 任务取消(但您不能等待您的 "cleanup" 块终止!)
如果你想使用 Redux,我建议选择更简单的东西,比如 async thunks。您可以拥有带 async-csp
and proper cancellation with cancellationtoken
.
的频道
假设我有一个函数
function* request(url) {
return global.fetch(url).then(response =>
_.result(response, 'json'))
}
两个代码示例对我来说都很好
const {data} = yield call(request, 'http://example.com/api');
yield put(actionSuccess(data));
和
const {data} = yield request('http://example.com/api');
yield put(actionSuccess(data));
故题。对 return 承诺的函数使用 call
效果有什么好处?
优点是可以看到应用程序中正在发生的事情。
您想知道状态更新的时间、原因和方式 [1].
call
[2] triggers an effect when a saga monitor is configured on the saga middleware before running the effect and after the effect is resolved.
在配置了 saga 监控器且未使用 call
效果的情况下,我在监控器日志中看到了这一点。
然而call
效果,
使用 call()
的一些好处:
- Testability, the only benefit the docs seem to mention (lower half of the page). I recommend redux-saga-test-plan,它允许通过直接提供 return 值来模拟
call()
和其他效果。 - 更细粒度task cancellation. Sagas are ES6 generators, which only yield control (back to redux-saga middleware) when you use the
yield
statement. These are the only points where cancellation can happen. Ayield call()
gives redux-saga an opportunity to cancel the task right before it's about to make that no longer needed call. Task cancellation is performed usinggen.return()
generator method. [1] [2] - 总体上更细粒度的任务调度。 Redux-saga 的并发模型基本上是协作式多任务处理。而 JavaScript 是单线程的。 A 因此,当你
yield
时,这是 redux-saga 中间件执行任何类型的其他任务调度的唯一机会。 (不过,我不确定 redux-saga 在这方面有多好。)
有关更多信息,我认为最好在 redux-saga 的 Github 上开一个问题直接询问维护者。
更新: 由于这个答案越来越受到关注:使用 redux-saga 前请三思。 它提供:
- 现在已过时的基于生成器的
async/await
版本自 ES7 可用
- CSP (频道) Golang用户应该熟悉
- 任务取消(但您不能等待您的 "cleanup" 块终止!)
如果你想使用 Redux,我建议选择更简单的东西,比如 async thunks。您可以拥有带 async-csp
and proper cancellation with cancellationtoken
.