首页 > 解决方案 > 异步/等待调用功能的最佳方式?

问题描述

我想知道是否可以从第二次或第三次通话中删除一些等待。

async componentDidMount() {
    try {
        let response = await this.CollectionManager.GetAllCollections();
        if(!response.error) {
            this.setState({collections: response.data});
        }
    } catch (e) {
        this.notify("Error can't connect with the server.");
    }
}

async GetAllCollections() {
    try {
        return await this.httpClient.Get(this.api_url);
    }  catch (e) {

    }
}

async Get (url) {
   try {
       return this.Response(await Axios.get(this._apiUrl + url));
   } catch (e) {

   }
}

我在互联网上找不到更多示例。

标签: javascriptasync-await

解决方案


我认为您需要从中删除等待componentDidMount

并阅读这篇文章也许可以帮助你https://javascript.info/async-await


推荐阅读