Ay_mhwan
Ay_mhwan

Reputation: 185

Can we use race with takeEvery ?

I am using saga and I try to achieve a race between a takeEvery and a timeout. If a user is not doing an action I want to dispatch the DISMISS action (with the time out). but if the user have already done the action I don't want to dispatch the DISMISS action.

I didn't succeed so my question is, can I do a race with a takeEvery ?

const id = 5 // just for the example
const {a, b} = yield race({
  a: yield takeEvery(DISMISS, function* ({payload}) {
    if (payload === id) {
      // find a way to win the race
    }
    // continue to listen
  }),
  b: yield call(delay, 6000),
})

Upvotes: 2

Views: 403

Answers (1)

Ay_mhwan
Ay_mhwan

Reputation: 185

I found the solution you shouldn't use takeEvery but use a normal call with a while:

answer:

const {timeout} = yield race({
  _: call(yourFunction$, param),
  timeout: call(delay, alertDelay),
})

with:

function* yourFunction$(param) {
  let yourCond = true
  while (yourCond) {
   const action = yield take(YOUR_ACTION_TYPE)
   // do what you want 
  }
}

Upvotes: 2

Related Questions