1

I have sagas:

function* sagaA() {         // accumulates all user requests
   // do stuff
   yield all(users.map((user) => call(sagaB, user));
   yield put(SUCCESS ACTION);
}

function* sagaB(user) {     // is responsible for single request
   yield call(api, user);
   // do stuff
}

function* watch() {       // watches for the action
   yield take('REQUEST', sagaA);
}

However, in this case the requests are parallel, they are not queued.

Question: how to modify this sagas so they are being called one by one?

Patrickkx
  • 1,740
  • 7
  • 31
  • 60

1 Answers1

2

You can use regular for loop:

function* sagaA() {
  for(const user of users) {
    yield call(sagaB, user);
  }
  yield put(SUCCESS_ACTION);
}
Aleksey L.
  • 35,047
  • 10
  • 74
  • 84
  • Unfortunately none of your answers/comments solved my main problem related with dealing with task cancellation if one of the calls failed. Ive made a bounty on my question, maybe you'd be interested in leaving a whole answer? https://stackoverflow.com/questions/63619471/do-not-fail-whole-task-even-if-one-of-promises-rejected – Patrickkx Sep 01 '20 at 16:18
  • Alex, consider adding an answer in my bounty question. I figured out what to do and how. Your answer above was kinda helpful. If you add a similar answer there I will mark it. Just make sure to add try catch there around for loop – Patrickkx Sep 01 '20 at 22:56
  • @Patrickkx please try editing the sandbox I've posted to reproduce the issue – Aleksey L. Sep 02 '20 at 06:06
  • 1
    Yeah, there were some details to be fixed, but now I got it working. Just add the answer in my bounty question ok? – Patrickkx Sep 02 '20 at 09:49
  • Why did you remove your answer? I wanted to mark it today – Patrickkx Sep 06 '20 at 11:36
  • It is basically the same as the already existing one. Originally I was wrong regarding try/catch around the `all` effect, it should wrap the `call`. I've added a bit cleaner example. Have a look at the comment https://stackoverflow.com/questions/63619471/do-not-fail-whole-task-even-if-one-of-promises-rejected#comment112753680_63694966 – Aleksey L. Sep 06 '20 at 11:58
  • But I dont want to award the existing answer because it basically didnt help me at all... Undelete your answer so I can mark it. – Patrickkx Sep 06 '20 at 12:56