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

Possible race condition?

gernot-at-etron opened this issue · comments

https://github.com/gernot-at-etron/redux-saga-put-async

Is it possible with the current code that the handler of yield put(decrementAction()); reaches this line this.data.push(data); before the handler of yield put(incrementAction()); does reach it?

Thank you

Greetings! Could you please link me the file where this.data.push(data); is called?

Expecting a handler to finish before the next handler when two consecutive yield put() are called can result in race conditions. All yield put() does under the hood is call store.dispatch(action). The takeEvery watcher for the actions you dispatched then get called but in a non-blocking way. My recommendation is to not use yield put in cases where you need the subsequent operation to complete and instead call the sagas directly.

In your case:

export function* start() {
	console.log("start()");
        yield call(increment);
	yield call(decrement);
}

export function* increment() {
	console.log("increment()");
        yield put(incrementAction());
	const singleton = Singleton.getInstance();
	yield singleton.funcA("increment");
}

export function* decrement() {
	console.log("decrement()");
        yield put(decrementAction());
	const singleton = Singleton.getInstance();
	yield singleton.funcA("decrement");
}

export function* watchIncrement() {
	yield takeEvery('START', start);
}

export function* rootSaga() {
	yield all([
		helloSaga(),
		watchIncrement()
	])
}