-3

my problem is that I want that my second function wait for the end of the exection of the first function, (i want the first point, before call the routing) but that seems not working. I'am quite new to coding with this, and i would appreciate any help and advise.

the code is :

async function waitForLoc(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            latUser = (position.coords.latitude) ;
            lngUser = (position.coords.longitude);
            var marker = L.marker([latUser, lngUser]).addTo(mymap);
        })
    }else console.log("geolocation does not work");
    return 1;
}
waitForLoc();

lat = coordinates[1];
lng = coordinates[0];

async function f() {
    let promise = new Promise((resolve, reject) => {
    setTimeout(() => resolve("done!"), 1000)
    });

    let result = await promise; 

    if(Route){
        mymap.removeControl(Route);
    }

    Route = L.Routing.control({
        waypoints: [
            L.latLng(latUser, lngUser),
            L.latLng(lat, lng)
        ],
        routeWhileDragging: false
    }).addTo(mymap);

    mymap.closePopup();
}
f();
whitek
  • 1
  • 1

1 Answers1

1

The async keyword allows you to use await inside a function and, as a side effect, forces that function to return a promise.

It doesn't detect the presence of a callback and automatically promisify that. You have to do that manually. If you do that then you can just return that promise, there is no point is using async unless you are awaiting something.

It doesn't cause code outside itself to automatically await the promise it returns. Again, you have to do that explicitly.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335