Redux重击:从已调度 *** 作中返回承诺

Redux重击:从已调度 *** 作中返回承诺,第1张

Redux重击:从已调度 *** 作返回承诺

当然,您可以从异步 *** 作中返回承诺:

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.      });}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4907945.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存