Since preemptive multitasking is not available in a browser, and JavaScript is inherently single threaded, how does a Redux Middleware like redux-saga handle infinite loops not designed for cooperative multitasking without triggering the long-running script dialog?
function* watchSaga() {
while (true) {
yield take(SOME_REQUEST);
// do something
}
}
Edit
My statement "not designed for cooperative multitasking" was wrong. A generator function's code is executed only until the first yield expression.
This while is not an infinite loop, it's a generator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators.
The yield keyword exits the function, but its state, including the last executed line, remains until the next time the function is called, when it restarts at the statement following the last executed line until it sees the yield keyword again.
yield
is indeed the key, as it yields control, suspending the current generator function and returning a value to it.A simple example: