【发布时间】:2019-07-19 15:43:41
【问题描述】:
Mobx 官方文档指出,您可以对流返回的 Promise 调用 cancel。 https://mobx.js.org/best/actions.html 只是没有如何做到这一点的例子。
上下文: 在componentDidMount中调用异步动作,我们需要在componentWillUnmount中取消这个动作。还想 setState 说 ui 可以在 promise 解决后呈现。
componentDidMount() {
this._fetchRawEguide = this.props.combinedEguide.fetchRawEguide(null, true)
.then(() => {
this._fetchRawEguide = null;
this.setState({
loaded: true
});
})
}
componentWillUnmount() {
if (this._fetchRawEguide) {
this._fetchRawEguide.cancel();
}
}
Mobx 动作看起来像这样
@action
fetchRawEguide = flow(function*(date, redirectOnError = false) {
try {
const res = yield request(...);
当它试图调用它时遇到了 .cancel() 不存在的问题。
我尝试将 when() 与常规 await / async 一起使用,但它似乎不起作用。如果有人有一个 await / async 的例子,那就太好了。
【问题讨论】:
-
我在想完全和我读到的一样,所以感谢你发布这个问题,然后回答它!
标签: reactjs mobx mobx-react