Redux 如何先获取axios结果,然后发送操作?

Redux 如何先获取axios结果,然后发送操作?,redux,redux-thunk,axios,Redux,Redux Thunk,Axios,这是原产地代码: export function startGame() { return function(dispatch) { axios({ method: 'post', url: '/api/actions/game/', data: {'game':'start'}, headers: getHeaders(), }) .then(response

这是原产地代码:

export function startGame() {
    return function(dispatch) {
        axios({
          method: 'post',
          url: '/api/actions/game/',
          data: {'game':'start'},
          headers: getHeaders(),
        })
        .then(response => {
          if(response.status===200){
            dispatch({
              type: TYPE.START_GAME,
            });
          }
        })
        .catch((error) => {
            dispatch({
                  type: TYPE.ERROR,
                });
        });
    }
}
我想要的是首先得到api结果,然后决定下一步要做什么(因为我有许多操作都调用相同的api)
我的逻辑如下,但我不知道如何使其工作
请帮帮我

export function startGame() {


    let result =  function(dispatch) {
        axios({
          method: 'post',
          url: '/api/actions/game/',
          data: {'game':'start'},
          headers: getHeaders(),
        })
        .then(response => {
          if(response.status===200){
            return {
                "result" : "OK",
                "data" : response.data
            }
          }
        })
        .catch((error) => {
            return {
                "result" : "FAIL",
                "data" : error
            }
        });
    }


    if result.result === "OK" {
        dispatch(someAction())
    }else{
        dispatch(otherAction())
    }


}

我不知道为什么不能在axios回调中调度
someAction
otherAction
。为什么这不适合你

export function startGame() {
      return function(dispatch) {
        axios({
          method: 'post',
          url: '/api/actions/game/',
          data: {'game':'start'},
          headers: getHeaders(),
        })
        .then(response => {
          if (response.status === 200) {
            dispatch(someAction(response.data));
          }
        })
        .catch((error) => {
            dispatch(otherAction(error));
        });
    }
}
如果要在别处定义API调用函数,可以执行以下操作:

// In some other file, say api.js
export function startGameApiCall() {
  return axios({
    method: 'post',
    url: '/api/actions/game/',
    data: {'game':'start'},
    headers: getHeaders(),
  });
}

// In your actions file
import { startGameApiCall } from './api';

export function startGame() {
  return function (dispatch) {
    startGameApiCall()
      .then(response => dispatch(someAction(response.data)))
      .catch(() => dispatch(otherAction()));
  }
}

我还将根据您axios请求的结果进行调查并发送另一个操作。

非常感谢您,这就是我想要的