我有一个使用redux-thunk的应用程序,我想试试redux-observable。目前,我有一个thunk操作创建器,它执行以下操作:
const initState = () => {
return dispatch => new Promise((resolve,reject)=>{
//these each return axios promises as they get data from the server
const pr1 = dispatch(processData1())
const pr2 = dispatch(processData2())
Promise.all([pr1,pr2])
.then(r=>resolve(true))
.catch(e=>reject(e))
})
}
}这是从我的componentDidMount组件中调用的,如下所示:
this.props.initState()
.then(r=>this.setState({loaded:true}))
.catch(e=>this.setState({loadError:true}))现在我想尝试redux-observable,我是否可以将它与redux-thunk结合起来,并替换processData2()调用来分派一个由redux-observable处理的操作?即
const pr2 = dispatch({type:OBSERVABLE_PROCESSDATA2})现在,据我所知,这不会返回一个承诺,所以原始代码现在将中断,因为在Promise.all中将立即.then(r=>resolve(true)),即使redux-observable epic还没有处理它。那么,有没有一种方法可以等待调度来完成史诗中的异步工作,或者在这种情况下,我基本上需要一直使用redux-observable?
发布于 2018-10-17 05:54:36
您可以在同一项目中组合redux-thunk和redux-observable。
操作-> redux-thunk -> redux-observable > store
但是redux-observable的概念是action$ in和action$ out。所以你需要在epics中编写所有的异步逻辑,并且不能访问返回的observable,也就是action stream。
所以要么你想把整个initState转换成一个史诗,要么直接在这个thunk中使用rx.js,而不是创建史诗
https://stackoverflow.com/questions/49978879
复制相似问题