Redux thunk: return promise from dispatched action

断了今生、忘了曾经 提交于 2019-12-13 12:04:46

问题


Is it possible to return promise/signal from action creator, resolved when Redux thunk has successfully dispatched certain action?

Consider this action creator:

function doPost(data) {
    return (dispatch) => {
        dispatch({type: POST_LOADING});
        Source.doPost() // async http operation
            .then(response => {
                dispatch({type: POST_SUCCESS, payload: response})
            })
            .catch(errorMessage => {
                dispatch({type: POST_ERROR, payload: errorMessage})
            });
    }
}

I want to call some function asynchronously in the component after calling doPost action creator when Redux has either dispatched POST_SUCCESS or POST_ERROR actions. One solution would be to pass the callback to action creator itself, but that would make code messy and hard to grasp and maintain. I could also poll the Redux state in while loop, but that would be inefficient.

Ideally, solution would be a promise, which should resolve/reject when certain actions (in this case POST_SUCCESS or POST_ERROR) gets dispatched.

handlerFunction {
  doPost(data)
  closeWindow()
}

The above example should be refactored, so closeWindow() gets called only when doPost() is successful.


回答1:


Sure, you can return promise from async action:

function doPost(data) {
    return (dispatch) => {
        dispatch({type: POST_LOADING});
        // Returning promise.
        return Source.doPost() // async http operation
            .then(response => {
                dispatch({type: POST_SUCCESS, payload: response})
                // Returning response, to be able to handle it after dispatching async action.
                return response;
            })
            .catch(errorMessage => {
                dispatch({type: POST_ERROR, payload: errorMessage})
                // Throwing an error, to be able handle errors later, in component.
                throw new Error(errorMessage)
            });
    }
}

Now, dispatch function is returning a promise:

handlerFunction {
  dispatch(doPost(data))
      // Now, we have access to `response` object, which we returned from promise in `doPost` action.
      .then(response => {
          // This function will be called when async action was succeeded.
          closeWindow();
      })
      .catch(() => {
          // This function will be called when async action was failed.
      });
}


来源:https://stackoverflow.com/questions/38329921/redux-thunk-return-promise-from-dispatched-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!