Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgja authored Nov 22, 2024
1 parent 7c5941d commit 84fef8a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,16 @@ const state = create(Promise.resolve(0))
const state = create(() => Promise.resolve(0))
```
And when setting state when initial value is promise, set is always sync.
But as in react there are two methods how to set a state. Directly `.set(2)` or with a function `.set((prev) => prev + 1)`.
#### Behavior with an Async Initial Value:
So how `set` state will behave with async initial value?
1. Directly call `.set(2)` will be sync, and will set the value to 2 (it will cancel the initial promise)
2. Call `.set((prev) => prev + 1)` will wait until previous promise is resolved, so previous value in set callback is always resolved.
##### Direct Call (.set(value))
Example: .set(2)
This is always synchronous. The state will immediately be updated to 2, and the pending promise (initial value) will be canceled or ignored.
##### Function Call (.set((prev) => ...))
Example: .set((prev) => prev + 1)
Calling of the set will be scheduled and wait until the previous promise is resolved.
### Debugging
`Muya` in dev mode automatically connects to the `redux` devtools extension if it is installed in the browser. For now devtool api is simple - state updates.
Expand Down

0 comments on commit 84fef8a

Please sign in to comment.