developit / developit/unistore

Race in the async action example?

Open
#165 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
2.8k
Forks
134
PR merge metrics
No merged PRs in 30d

Description

I'm wondering about this example in the README:

```
// Async actions can be pure async/promise functions:
async getStuff(state) {
let res = await fetch('/foo.json')
return { stuff: await res.json() }
},
```

I don't fully understand how you can really support async actions like in this example.

If `getStuff()` is called twice before the first call resolves, and the two `fetch()` operations happen to finish in reverse order (because HTTP servers are unpredictable) doesn't this cause a race condition?

Won't you end up with the `stuff` from the first request instead of the second?

I understand (from reading [this issue](https://github.com/developit/unistore/issues/3)) that the internal call to `setState()` is defferred until the promise resolves - but I don't think that's enough to guarantee that `setState()` calls are performed in the same order the actions were invoked?

For something like an auto-complete input fetching results from the server-side, it seems like there's a good chance of this actually happening.

Do I have to manually prevent races?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.