简体   繁体   English

如何在执行下一行代码之前使await订阅块完整?

[英]How can I make the await subscribe block complete before executing next line of code?

I am subscribing to Google Maps API to pull in coordinates based on an address. 我正在订阅Google Maps API以根据地址提取坐标。 My understanding is that by awaiting the subscribe line, it should wait for that code block to finish before moving onto the following lines. 我的理解是,通过等待订阅行,它应等待该代码块完成,然后再移至以下行。

  async getCoordinates(address) {
      let url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + encodeURIComponent(address) + '&key=' + environment.geocodeKey;
      let lat;
      let lng;
      let coord: Coordinate;
      await this.http.get(url).subscribe(data => {
          let map = data.json() as Results;
          lat = map.results[0].geometry.location.lat;
          lng = map.results[0].geometry.location.lng;
          console.log("inner lat is: " + lat)
      });
      console.log("outer lat is: " + lat)
      coord = new Coordinate(lat, lng);
      console.log("coord lat is: "+ coord.latitude)
      return coord;
  }

However when I run the app, I see this in the console: 但是,当我运行该应用程序时,我会在控制台中看到以下内容:

outer lat is: undefined
coord lat is: undefined
inner lat is: 38.912799

That is indicating to me that the code inside the await block is executing last. 这向我表明,等待块中的代码正在最后执行。 I have the same results without the async/await. 我没有异步/等待也有相同的结果。 How can I get the subscribe code to execute first and make the other code wait until my lat and lng have a value? 我如何才能使订阅代码首先执行,并使其他代码等到lat和lng都具有值? Right now they only have values inside the subscribe block, but I can't put a return line inside like this answer suggests . 现在,它们仅在subscribe块内具有值,但我不能像此答案所示那样在其中放入返回行。

I have read that await/async in Angular is essentially the same thing as a promise and callback. 我已经读到,Angular中的await / async本质上是与promise和callback相同的东西。 I am treating the result of this async getCoordinates function as a promise already by using .then(): 我已经通过使用.then()将这个异步getCoordinates函数的结果视为一个承诺:

service.getCoordinates(this.address).then(
    (val) => this.coordinates = val,
    (err) => console.log(err)
);

Angular's http service returns an Observable . Angular的http服务返回一个Observable You can convert this to a promise by using the rxjs toPromise operator: 您可以使用rxjs toPromise运算符将其转换为toPromise

import 'rxjs/add/operator/toPromise';

await this.http.get(url).toPromise().then()
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在执行下一行代码之前如何等待? - How do I wait before executing the next line of code? 如何在执行下一行代码之前等待Backbone事件触发器完成 - How to wait for Backbone event trigger to complete before executing next line of code async/await 在 JS 中执行下一行之前不等待 - async/await doesn't wait before executing next line in JS 如何让这个代码块在它之后的其他代码之前完成执行? - How do I make this code block to finish executing before other codes after it? 如何在执行代码之前等待slideToggle完成? - How do I wait for the slideToggle to complete before executing code? 在执行下一个代码之前打字稿会等待循环完成吗? - Will typescript wait for a loop to complete before executing the next code? ReactJS - 在执行下一个代码之前等待多个 setState 完成 - ReactJS - waiting for multiple setState to complete before executing next code await 函数在进入下一行之前没有执行。 (异步/等待不起作用) - await function is not executing before going on next line. (Async/await not working) 如何在执行下一个之前等待函数完成? - How to await Function to finish before executing the next one? 下一个代码块在获取之前执行,我该如何修复它 - Next block of code execute earlier before the fetch how can I fixed that
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM