【发布时间】:2018-06-04 16:45:03
【问题描述】:
我已经阅读了很多关于异步等待的内容,但显然我仍然不明白。 ;-)
我正在尝试将以下 .then 承诺结构转换为异步等待:
componentDidMount() {
const { store } = this.props
Promise.all([
API.fetchTodos(),
API.fetchGoals(),
]).then(([ todos, goals ]) => {
store.dispatch(receiveDataAction(todos, goals))
})
store.subscribe(() => this.forceUpdate())
console.log('test')
}
我的结果是:
async componentDidMount() {
const { store } = this.props
const [todos, goals] = await Promise.all([
API.fetchTodos(),
API.fetchGoals(),
])
store.dispatch(receiveDataAction(todos, goals))
store.subscribe(() => this.forceUpdate())
console.log('test')
}
结果是这个函数永远不会结束。它调用包括console.log 在内的所有内容,但随后程序就停止了(没有错误)。我没有向您展示应用程序的任何其他部分,因为根据我的理解,这两个功能应该是等效的 - 所以其余的应该无关紧要。显然我错了! :-) 我做错了什么,为什么我的解决方案不起作用?
【问题讨论】: