传递异步响应 "through" thunk

passing an async response "through" a thunk

是否可以通过 thunk 传递异步 "response"?给定以下动作创建者:

export function emitLoadPlot(json_req) {
  return function (dispatch) {
    axios.post(URI, {
      json_req
    })
    // .then(() => dispatch(
    //   emitTxRxAlert("yellow", "Received plot from server")
    // ))
    .then((response) => dispatch({
      type: EMIT_PLOT_DATA_REQ,
      payload: response
    }))
    .then(() => dispatch(
      emitTxRxAlert("green", "Loaded plot model")
    ))
    .then(() => dispatch({
      type: EMIT_LOAD_PLOT
    }))
    .then(() => dispatch({
      type: EMIT_VIEW_STATUS
    }))
  };
}

如果我取消注释 Alert dispatch,response 永远不会到达需要的地方。说得通。其中一些调用(以及客户端的操作)需要很长时间才能完成,我想在浏览器中的异步请求 returns 和当它加载到 redux 存储中时。在这台慢速笔记本电脑上,网络请求比处理 response.

花费的时间少得多

有什么方法可以让我通过上面的第一个 thunk 传递 response(或对 response 的引用)?

调用 then() return 是对回调结果的承诺。

您的回调 return 随便 dispatch() return,这不是您想要的值。

相反,您需要 return 原始值:

.then(response => {
  dispatch(...);
  return response;
})