在 redux saga 中使用和不使用调用方法有什么区别?
what is difference between using and not using call method in redux saga?
我只是想知道,在redux-saga生成器函数中,yield call(function, ...arg)
和yield function(...arg)
有什么区别?
这是我的一段代码,在生成器函数中:
const userRef = yield call(createUserProfileDocument, userAuth);
和
const userRef = yield createUserProfileDocument(userAuth);
有什么不同?两条线都按预期工作。
只是 yield + call
是使用 saga 的标准方式还是还有更多?
根据 official docs 不同之处在于 call
我们不会立即执行提取调用,相反,调用会创建效果描述.
Just as in Redux you use action creators to create a plain object
describing the action that will get executed by the Store, call
creates a plain object describing the function call. The redux-saga
middleware takes care of executing the function call and resuming the
generator with the resolved response.
这让我们可以轻松地在 Redux 环境之外测试 Generator。因为 call 只是一个 returns 普通对象的函数。
import { call } from 'redux-saga/effects'
import Api from '...'
const iterator = fetchProducts()
// expects a call instruction
assert.deepEqual(
iterator.next().value,
call(Api.fetch, '/products'),
"fetchProducts should yield an Effect call(Api.fetch, './products')"
)
现在我们不需要模拟任何东西,基本的相等性测试就足够了。
这些声明式调用的优点是我们可以通过遍历生成器并对连续生成的值进行深度相等测试来测试 Saga 中的所有逻辑。这是一个真正的好处,因为你复杂的异步操作不再是黑盒,无论它有多复杂,你都可以详细测试它们的操作逻辑。
我只是想知道,在redux-saga生成器函数中,yield call(function, ...arg)
和yield function(...arg)
有什么区别?
这是我的一段代码,在生成器函数中:
const userRef = yield call(createUserProfileDocument, userAuth);
和
const userRef = yield createUserProfileDocument(userAuth);
有什么不同?两条线都按预期工作。
只是 yield + call
是使用 saga 的标准方式还是还有更多?
根据 official docs 不同之处在于 call
我们不会立即执行提取调用,相反,调用会创建效果描述.
Just as in Redux you use action creators to create a plain object describing the action that will get executed by the Store, call creates a plain object describing the function call. The redux-saga middleware takes care of executing the function call and resuming the generator with the resolved response.
这让我们可以轻松地在 Redux 环境之外测试 Generator。因为 call 只是一个 returns 普通对象的函数。
import { call } from 'redux-saga/effects'
import Api from '...'
const iterator = fetchProducts()
// expects a call instruction
assert.deepEqual(
iterator.next().value,
call(Api.fetch, '/products'),
"fetchProducts should yield an Effect call(Api.fetch, './products')"
)
现在我们不需要模拟任何东西,基本的相等性测试就足够了。
这些声明式调用的优点是我们可以通过遍历生成器并对连续生成的值进行深度相等测试来测试 Saga 中的所有逻辑。这是一个真正的好处,因为你复杂的异步操作不再是黑盒,无论它有多复杂,你都可以详细测试它们的操作逻辑。