The accepted solution here didn't work for me. I am making a call to a location service that needs to be synchronous because an api call is executed on it immediately thereafter.
My logging indicates that the location service is still returning undefined
despite the await clause.
service that makes api call
...
@Injectable({providedIn: 'root'})
class PrepopulateService {
constructor(private locationService: LocationService,
private log: LoggerService) { }
async prepopulate(): Promise<boolean> {
const coords: string[] = await this.locationService.getLocation();
console.log(coords)
if(coords == null) {
return false;
}
console.log(coords)
// api call here
return true;
}
}
export { PrepopulateService }
service that gets location for it
...
@Injectable({providedIn: 'root'})
class LocationService {
constructor(private log: LoggerService) { }
getLocation(): Promise<string[]> {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const longitude = position.coords.longitude;
const latitude = position.coords.latitude;
console.log([String(latitude), String(longitude)])
return [String(latitude), String(longitude)];
});
} else {
this.log.warn('No support for geolocation');
return null;
}
}
}
export { LocationService }
What's wrong with my implementation of async/await?