Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples of race conditions #1003

Closed
tonypee opened this issue Nov 4, 2015 · 1 comment
Closed

Examples of race conditions #1003

tonypee opened this issue Nov 4, 2015 · 1 comment

Comments

@tonypee
Copy link

tonypee commented Nov 4, 2015

In your documentation, it is stated: "Because all mutations are centralized and happen one by one in a strict order, there are no subtle race conditions to watch out for"

Can you please give an example of how a race condition can occur? Given that JS is single threaded, every 'action' on the model will operate in a strict order (the execution order). Obviously, asynchronous functions can delay when a mutation occurs, but this is the same as an async action creator 'waiting' before dispatching an action (actions are processed synchronously).

Ive struggled to see how any flux implementation helps in this respect. All they do is allow for a 'change' event after the mutation.

Any help with what i'm conceptually missing would be great!

@gaearon
Copy link
Contributor

gaearon commented Nov 4, 2015

Obviously, asynchronous functions can delay when a mutation occurs, but this is the same as an async action creator 'waiting' before dispatching an action (actions are processed synchronously).

Yes. But even async action creator leaves a trace: a synchronous dispatch. If you log them, you can see how a race condition has occurred, if it has, and you can reproduce the exact same state if you replay actions in this order. Flux-when-you-don't-do-AJAX-from-Stores has the same benefits.

By contrast, with something like Backbone or Flux-when-you-call-AJAX-from-Stores, there is no way to reproduce a race condition. You might have a subtle bug due to requests sometimes coming in the wrong order, but unless there is a single synchronous point where everything has to be described as a plain object actions, there is no way for you to reliably reproduce such race conditions.

Of course we can't magically solve the race conditions, as any program is asynchronous (even user input is :-). But Redux makes them explicit, so if you ever get them, you know how to debug, log, and reproduce them.

@gaearon gaearon closed this as completed Nov 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants