【问题标题】:The best way for async/await call funtion?异步/等待调用功能的最佳方式?
【发布时间】:2018-09-28 23:14:50
【问题描述】:

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

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) {

   }
}

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

【问题讨论】:

  • 这取决于代码是否需要等待。这种“感觉”适合您的情况吗?
  • 因此,如果我删除指令二或三的等待,它会返回相同的结果吗?永不失败?
  • 不,它会失败。

标签: javascript async-await


【解决方案1】:

我认为您需要从 componentDidMount 中删除 await

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

【讨论】:

  • 方法是否异步无关紧要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
  • 2019-04-22
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
相关资源
最近更新 更多