0

I have a function that has to be async because I need it to sleep at some point for half a second, but then I'd just like it to be completely sequential. I'm doing the following:

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");
  }

I would like checkNearestStreetView to run completely before the code continues to run, so I'm trying to await it and only continue once it's done. But with the above code, checkNearestStreetView runs in async, and I get the following output on the console:

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

Meaning checkNearestStreetView only finishes running after everything else ran, even though I told the function to await it (and getPanoramaByLocation). There might be something I'm misunderstanding on the subject of await, please help me learn why this happens this way!

lte__
  • 7,175
  • 25
  • 74
  • 131
  • here - `await this.checkNearestStreetView.bind(this)` you are not calling the function, you are creating a new one with binded context Either call the function and pass an argument or use `await` inside `getPanoramaByLocation` (I don't know what the function do and what it expects) – RidgeA Sep 18 '18 at 12:54
  • `getPanoramaByLocation` is a Google webservice: http://branflake2267.github.io/GWT-Maps-V3-Api/javadoc/3.8.1/com/google/gwt/maps/client/streetview/StreetViewService.html#getPanoramaByLocation(com.google.gwt.maps.client.base.LatLng,%20double,%20com.google.gwt.maps.client.streetview.PanoramaByLocationHandler) I need to bind `.this` to the function as I intend to use some of the context variables within `checkNearestStreetView`. Could you please show me what you mean with code? Not sure I understand all the concepts yet. – lte__ Sep 18 '18 at 13:06
  • I don't know what do you want to do, but, I'm certain, you don't have to `await` result of `bind` function as it returns a new function and doesn't call original one. Probably you want to await the function the `bind` returns? You gave me a link to some Java library and I don't know what I have to see there. – RidgeA Sep 18 '18 at 13:26
  • You asked what `getPanoramaByLocation` is, I sent you the documentation for it. Yes, I want to await the function `checkNearestStreetView` with the context bound to it. How do I do that? – lte__ Sep 18 '18 at 13:30
  • you have to call it... – RidgeA Sep 18 '18 at 13:33
  • AFAIK I *am* calling it via callback. Could you please demonstrate via code? – lte__ Sep 18 '18 at 13:38
  • It gets called, I get the output on the console. It's just not called in the right order. – lte__ Sep 18 '18 at 13:39

0 Answers0