0

我在 componentdidmount 中使用 promise.all 从两个 api 获取 response.data。

constructor(){
    super();
       this.state = {
        data: [],
        p1: [],
        p2: []
       };
  }

  async componentDidMount() {
    const [res1, res2] = await Promise.all([
      axios.get(api1),
      axios.get(api2)
    ]);
    this.setState({
      p1: res1.data,
      p2: res2.data
    });
    console.log("this.state.p1" ,this.state.p1)
    console.log("this.state.p2" ,this.state.p2)
  }

有时 this.state.p1 和 this.state.p2 都返回正确的数据,但有时当我刷新我的页面时 this.state.p1 返回 this.state.p2 的数据并且 this.state.p2 变为空。

4

0 回答 0