javascript - Angular 中的异步编程

标签 javascript angular http asynchronous async-await

如何在 Angular 中使用 async/await 来发出异步请求。例如,调用函数 postDataToServer(myData) 并且仅在其解析后调用 getDataFromServer()?我已经阅读了示例,但仍然没有理解它,如果我能看到一个基本示例,其中您仅在第一个请求完成后才发出一个 http 请求,然后再发出另一个请求,那将有很大帮助。

编辑:我的http请求返回可观察值而不是 promise ,也许async/await不是正确的选择?

最佳答案

好吧,如果您正在起诉 HttpHttpClient,那么 postget 方法将返回 observable,您可以订阅,并且回调将始终异步执行。

this.http.post(url, body).subscribe(
   (res) => { 
      // you can just simply call another function which will make request
      this.getData();
      // or just make another request
      this.http.get(url).subscribe(...);

   }


private getData() {
   this.http.get(url).subscribe(...)
}

关于javascript - Angular 中的异步编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47945835/

相关文章:

http - 是否可以在 NSIS 中使用 http get 和 http post?

http - 如何传递 'XML' 输入以使用 Http 适配器在 IBM Mobile First 中使用 API

javascript - PHPStorm(苹果机): Cannot debug --> Chrome keeps waiting for localhost

javascript - 调用 Web API - 无法读取未定义的属性 'filter'

angular - 为什么 *NgIf 不能使用 bool 值工作 angular 9

javascript - 错误 : Permission denied to access property "rejection" in angler 2

c - 无法从 C 中的 HTTP 请求获取路径和方法

javascript - 穆尔特 : Setting different destinations in the destination Property

javascript - 使用javascript在android chrome浏览器上打开前置摄像头

javascript - 如何将 html5 Canvas 图像保存到 mssql varbinary(max) 字段?