Skip to content

redux-thunk not waiting for my promise to be fulfilled #52

@alexanderharm

Description

@alexanderharm

I have the following setup following your examples for chaining async actions:

// final action
function finalAction (status) {
  return {
    type: 'AUTH_STATUS'
    , status
}

// thunk action
function thunkAction () {
  return (dispatch, getState) => {
    return getStatusApi()
    .then((status) => {
      return dispatch(finalAction(status))
    })
    .catch((err) => {
      return dispatch({type: 'AUTH_ERROR'})
    })
  }
}

// api function returning another promise
function getStatusApi () {
  return myAsyncApiCall().then((res) => {
    return res.status
  })
}

// dispatch
store.dispatch(thunkAction())
.then(() => {
  // the end
})

What happens is that the flow is like this:

store.dispatch
thunkAction
getStatusApi
end
myAsyncApiCall
finalAction
reducer

For one reason or another store.dispatch resolves before myAsyncApiCall is resolved.
I either receive then a TypeError or if wrapping the whole into a promise an undefined action error.
Any idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions