当然,您可以从异步 *** 作中返回承诺:
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) }); }}
现在,
dispatch函数返回了一个承诺:
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. });}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)