Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux saga: What is the difference between using yield call() and async/await?

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!

like image 644
gkeenley Avatar asked May 19 '19 19:05

gkeenley


1 Answers

'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

like image 160
SalientBrain Avatar answered Oct 04 '22 22:10

SalientBrain