0

I am using sagas to track multiple async tasks, but there is one problem I haven't been able to completely solve:

function* performTask1() {
    // Some logic here to takeLatest for the relevant component

    // check if component id matches?

    // Only perform API call with the latest
    const { result } = yield takeLatest('doAsync2')
}

function* performTask2() {
    const { result } = yield call(api, args)
    // do something with results (not relevant)
}

function* watchAsyncTasks() {
    yield takeEvery('doAsync2', performTask2)
    yield takeEvery('doAsync1', performTask1)
}
  1. componentA dispatches doAsync1
  2. componentB dispatches doAsync1

  3. component C dispatches doAsync2 (for good measure)

  4. componentA dispatches doAsync1

  5. componentB dispatches doAsync1

How can I use sagas to ensure that only sagas 3, 4, and 5 complete their API call?

Anthony Chung
  • 1,467
  • 2
  • 22
  • 44
  • What you mean by `complete their API call`? you can check this with dispatch success action from those `sagas` right – Thaadikkaaran Dec 13 '16 at 12:37
  • To complete an api call means to arrive back from server with data. The problem that I did not make clear enough is that we don't want the results from action 1 arriving before that of action 4 (which we can't guarantee just because 1 fires before 4). We either want to process 1 and 4 in order OR ensure that we only take the results of 4 – Anthony Chung Dec 13 '16 at 16:00
  • 1
    maybe just use takeLatest instead of takeEvery? – WuTheFWasThat Dec 13 '16 at 21:29

1 Answers1

1

function* generator(){
    yield call(api,params);
    yield call(api2, params2);
}

const gen = generator;

gen.next() // done: false/true
gen.next() // done: false/true
Marty
  • 534
  • 8
  • 24
  • It's answer, maybe :) My question is here: https://stackoverflow.com/questions/52215425/redux-saga-watcher-not-working-correctly-what-can-be-a-problem – Marty Sep 07 '18 at 07:19
  • Oh sorry I've seen triage and I thought that it is question. – propoLis Sep 07 '18 at 08:49