redux-saga / redux-saga

An alternative side effect model for Redux apps

Home Page:https://redux-saga.js.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a standard way to break while true loops with call effect when END is dispatched?

krutoo opened this issue · comments

Hi, first of all thanks for great library.

In my case i have many while true loops and i want to break them all when end is dispatched.

Unfortunately, this loops dont breaks when END is dispatched.

There is the example:

function* main () {
  yield fork(infiniteUpdate);
}

function* infiniteUpdate () {
  while (true) {
    yield call(delay, 1000);
    yield call(update);
  }
}

function delay (ms) {
  return new Promise(done => setTimeout(done, ms))
}

function update () {
  console.log('update called');
}

when i call sagaMiddleware.run(main) it start log every second.

after I dispatch the END it still continues to logs.

Is there a standard way to break while true loops with call effect where END is dispatched?

I was offered a good solution on discord from vijah:

while (true) {
  yield race([take(END), call(someAsyncFn)]);
}

But the question remains: is this behavior a bug or a feature?