Skip to content

createAsyncThunk docs imply payloadCreator executes before pending action #416

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

Closed
dlwalsh opened this issue Mar 10, 2020 · 2 comments
Closed

Comments

@dlwalsh
Copy link
Contributor

dlwalsh commented Mar 10, 2020

I understand createAsyncThunk executes in the following order:

  1. Pending action
  2. Payload creator
  3. Fulfilled/rejected action

However this code snippet implies something different:

const fetchUserById = createAsyncThunk(
  'users/fetchByIdStatus',
  async (userId, { getState }) => {
    const { loading } = getState().users
    if (loading !== 'idle') {
      return
    }
    const response = await userAPI.fetchById(userId)
    return response.data
  }
)

The loading flag is expected to be in the idle state. But by the time the payload creator is executed, the loading flag will have been changed to pending.

    [fetchUserById.pending]: (state, action) => {
      if (state.loading === 'idle') {
        state.loading = 'pending'
      }
    },

Is this a bug in the example code?

@markerikson
Copy link
Collaborator

markerikson commented Mar 10, 2020

Hmm. Yeah, that does seem like a bug in the snippet. The hazards of trying to write snippets that aren't necessarily executable :)

Can you file a PR against the v1.3.0-integration branch to change it to something more appropriate? Probably something like if(loading === 'pending') return.

@dlwalsh
Copy link
Contributor Author

dlwalsh commented Mar 22, 2020

Resolved by #417

@dlwalsh dlwalsh closed this as completed Mar 22, 2020
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