Last active
March 1, 2020 17:25
-
-
Save Godefroy/460f6d484a8da0b3267d9e87ddcc09bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default prepareStore(initialState, { | |
// Action "incrementAsync" that increments multiple times | |
// We're using an async generator to yield multiple partial states | |
// and execute asynchronous operations | |
async *incrementAsync(n) { | |
// We can access the current state with "this" | |
// We shouldn't need this too often. It's like thunks' getState() | |
console.log('initial count', this.count) | |
// Instead of dispatching an action as w're used to with redux-thunk, | |
// we can yield a partial state at any moment | |
yield { counting: true } | |
for (let i = 1; i <= n; i++) { | |
// In order to be really asynchronous, we need to increment | |
// the current state and not the initial state. | |
// We can use a function that takes the state as an argument | |
yield state => ({ count: state.count + 1 }) | |
// Wait for 500ms | |
await new Promise(resolve => setTimeout(resolve, 500)) | |
} | |
yield { counting: false } | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment