Принятое решение здесь у меня не сработало. Я звоню в службу определения местоположения, которая должна быть синхронной, потому что сразу после этого на нем выполняется вызов API.
Моя регистрация указывает, что служба определения местоположения все еще возвращает undefined
несмотря на предложение await.
сервис, который делает вызов API
...
@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 }
сервис, который находит местоположение для него
...
@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 }
Что не так с моим реализация async / await?