这是我在songAction.js我该如何使redux发送一个动作并返回一个承诺?
export function createSong(title, url, token) {
axios.defaults.headers.common['Authorization'] = token
return function (dispatch) {
axios.post('http://localhost:8080/api/song/create', {
title,
link: url
})
.then((response) => {
console.log('the response was', response)
if(response.data.success){
dispatch({type: "CREATE_SONG_FULFILLED", payload: response.data.song})
} else {
dispatch({type: "CREATE_SONG_REJECTED", payload: response.data})
}
})
.catch((err) => {
dispatch({type: "CREATE_SONG_REJECTED", payload: err})
})
}
}
功能我希望能够派遣这样我就可以使用函数这样一个组件内后返回一个承诺 -
createSong(title, url, token)
.then((result) =>{
// do stuff with result
})
我知道我可以通过回调使这项工作异步..但我想使用承诺的ES6功能。我有点困惑,我该如何做到这一点。
以及既不'函数(派遣){''也没有。然后((响应)=> {'返回任何东西,所以这是一个开始的问题 –
只需从axios中返回:'return axios.post('http:// localhost:8080/api/song/create'...' –