admin管理员组文章数量:1386782
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 ponent 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.
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 ponent 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.
Share Improve this question asked Jul 12, 2016 at 13:11 Tuomas ToivonenTuomas Toivonen 23.6k52 gold badges145 silver badges243 bronze badges1 Answer
Reset to default 8Sure, 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 ponent.
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.
});
}
本文标签: javascriptRedux thunk return promise from dispatched actionStack Overflow
版权声明:本文标题:javascript - Redux thunk: return promise from dispatched action - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744493192a2608858.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论