如何依次调用redux操作

时间:2019-03-17 18:42:04

标签: javascript reactjs redux promise react-redux

我知道我需要使用Promise,但是不能将其放入代码中以实现我想要的。

所以我有3个redux动作:

getAllCoins()
getUserCoins()
setUserCoinPortfolio(a, b)

set操作需要两个get操作的返回值,因此需要在这些功能完成后调用它。两者都可以同时被解雇。这是我发起的两个呼叫:

 refresh = async () => {
    this.setState({refreshing: true});
    this.props.getAllCoins();
    this.props.getUserCoins();
    this.setState({refreshing: false});
};

componentDidMount = () => {
    this.refresh();
}

我应该将这些合并为一个动作吗?我应该将它们分开,但是如何构造调用?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

As you are using asyn you can wait for first two actions and once those completed you can call set. Something like below.

refresh = async () => {
    this.setState({refreshing: true});
    await this.props.getAllCoins();
    await this.props.getUserCoins();
    this.setState({refreshing: false});
};

or

refresh = async () => {
    this.setState({refreshing: true});
    await Promise.All([this.props.getAllCoins,this.props.getUserCoins]);
    this.setState({refreshing: false});
};