0

我有一个必须是异步的函数,因为我需要它在某个时候休眠半秒,但我希望它是完全顺序的。我正在执行以下操作:

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

checkNearestStreetView(panoData){
  if(panoData){
    if(panoData.location){
      if(panoData.location.latLng){
        console.log("new panodata" + panoData.location.latLng);
        this.currentpos = panoData.location.latLng;
        console.log("new panodata:" + this.currentpos);
      }
    }
  } else {
    this.currentpos = null;
    console.log("no new panodata");
  }
}    

async updateLocation(feature, layer) {
      console.log("Oh");
      await sleep(500);
      console.log("Hi");
      console.log("Mark!");

      var webService = new google.maps.StreetViewService();  
      var checkaround = 50;

      ...

      console.log("curentpos before checknearest " + this.currentpos);
      await webService.getPanoramaByLocation(this.currentpos, checkaround, await this.checkNearestStreetView.bind(this));
      console.log("curentpos after checknearest " + this.currentpos);      
    console.log("success");
  }

我想checkNearestStreetView在代码继续运行之前完全运行,所以我正在尝试await它,只有在它完成后才能继续。但是使用上面的代码,checkNearestStreetView异步运行,我在控制台上得到以下输出:

Oh
Hi
Mark! 
curentpos before checknearest (42.59678542, -76.15251434999999) 
curentpos after checknearest (42.59678542, -76.15251434999999)
success
no new panodata

意思是只有在其他所有东西都运行后才checkNearestStreetView结束运行,即使我告诉它函数(和)。我可能对这个主题有误解,请帮助我了解为什么会这样!awaitgetPanoramaByLocationawait

4

0 回答 0