reduxjs / redux

A JS library for predictable global state management

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

concurrency issue when promises resolve at the same time

ITzhaos opened this issue · comments

I'm trying to use redux, but there is a concurrency issue which is not mentioned in the doc.

my reducer:

function counterReducer(state = { value: 0 }, action) {
    switch (action.type) {
        case 'incremented':
            return { value: state.value + 1 }
        case 'decremented':
            return { value: state.value - 1 }
        default:
            return state
    }
}

dispatch ussage:

urlsArr.foreach(url => {
	axios.post(url).then(response => {
		store.dispatch({ type: ''incremented''})
	});
});

will there be an error in the state of value if the requests return at the same time ?

It is difficult to request simultaneous return on a mobile phone, but I am worried that second action comes, but the first one is still dispatching.

does Redux have any measures to ensure that this problem does not occur?

Only one JS function can execute at a time - that's how the language and runtime work. It's impossible to have a second dispatch outside the store in this case while the first one is still happening.