【发布时间】:2016-12-30 06:38:48
【问题描述】:
这里是源代码:
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())
}
}
【问题讨论】:
标签: redux redux-thunk axios