I'm using a Redux Saga template and it uses generator functions that contain instances of yield call()
. Basically it looks like this:
function *a(){
yield call(<some function>);
}
yield takeLatest(SOME_ACTION, a)
My questions are as follows:
1) Am I correct that the reason for using
function *a(){
yield call(<some function>());
}
instead of
function a(){
<some function>()
}
is that with the first one, the generator function will wait until the yield call
line has returned before proceeding, whereas in the second one, () will be called asynchronously?
2) If I'm correct about (1) ^^, then calling some_function
inside a
seems the same as the following:
async a() {
await some_function();
}
Is this correct?
3) If I'm correct about (2) ^^, then it seems like the only reason to use generator functions instead of async/await is that generator functions can be called from yield takeLatest
etc. Is this correct? Or is there some other rational behind it?
Thanks!
'yield' will wait until generator is unrolled to this 'yield' iteration.
The idea of saga generator (worker) is to generate special functions (so called 'effects') using effect creators (put, call etc), not just do something. This allows to orchestrate function calls.
See also Redux Saga async/await pattern
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With