Angular 承诺和等待

Angular 承诺和等待,angular,typescript,promise,Angular,Typescript,Promise,我正在使用typescript,并试图获得我们目前所在的地理位置。我可以获取位置,但我的代码继续,没有设置位置。我决定用等待和承诺。我创建了一个如下所示的服务: @Injectable() export class GoogleMapsService { private latitude: number; private longitude:number; public currentLongitude(): number { return this.longitude; } pu

我正在使用typescript,并试图获得我们目前所在的地理位置。我可以获取位置,但我的代码继续,没有设置位置。我决定用等待和承诺。我创建了一个如下所示的服务:

@Injectable()
export class GoogleMapsService  {

private latitude: number;
private longitude:number;

public currentLongitude(): number {
    return this.longitude;
}
public currentLatitude(): number {
    return this.latitude;
}

constructor() {
    this.setCurrentLocation();
}

private async setCurrentLocation() {
    let location: GoogleLocation = await this.getLocation();

    this.latitude = location.latitude;
    this.longitude = location.longitude;
}


private getLocation(): Promise<GoogleLocation> {
    let promise = new Promise<GoogleLocation>(() => {
        let location = new GoogleLocation();
        navigator.geolocation.getCurrentPosition(position => {
            location.latitude = position.coords.latitude;
            location.longitude = position.coords.longitude;
        });
        return location;
    });

    return promise;
    }
}

所以我的问题是,在我等待它的时候,我如何设置它。所以,当我尝试访问它时,它就在那里?

你永远不会在getLocation中解决你的承诺,所以等待自然会永远等待。从promise executor返回值传递到新promise的函数不会解析该承诺,请注意,您试图返回的内容是在坐标填充之前返回的

相反,在promise executor函数中接受resolve和reject参数并使用它们:

private getLocation(): Promise<GoogleLocation> {
    return new Promise<GoogleLocation>((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(position => {
            if (/*...it didn't work...*/) {
                reject(new Error(/*...*/));
            } else {
                // It worked
                const location = new GoogleLocation();
                location.latitude = position.coords.latitude;
                location.longitude = position.coords.longitude;
                resolve(location);
                // Maybe you could just `resolve(position.coords)`?
            }
        });
    });
}

旁注:如果您承诺使用地理定位服务,您根本不需要新的承诺。

您永远不会在getLocation中解决您的承诺,因此自然等待将永远等待。从promise executor返回值传递到新promise的函数不会解析该承诺,请注意,您试图返回的内容是在坐标填充之前返回的

相反,在promise executor函数中接受resolve和reject参数并使用它们:

private getLocation(): Promise<GoogleLocation> {
    return new Promise<GoogleLocation>((resolve, reject) => {
        navigator.geolocation.getCurrentPosition(position => {
            if (/*...it didn't work...*/) {
                reject(new Error(/*...*/));
            } else {
                // It worked
                const location = new GoogleLocation();
                location.latitude = position.coords.latitude;
                location.longitude = position.coords.longitude;
                resolve(location);
                // Maybe you could just `resolve(position.coords)`?
            }
        });
    });
}

旁注:如果你承诺提供地理定位服务,你根本不需要新的承诺。

所以我的问题是,在我等待的时候,我如何设置它-你想等待什么?让location:GoogleLocation=等待这个。getLocation;所以我的问题是,在我等待它的时候,我如何设置它-你想等待什么?让location:GoogleLocation=wait this.getLocation;谢谢tj,你能给我推荐一个我能读到的关于如何做到这一点的链接吗?@3xGuy:看起来还不错。很好,我注意到它现在链接到了他写的一本在线书……谢谢tj,你能给我推荐一个链接,让我读一下如何做到这一点吗?@3xGuy:看起来还不错。很好,我注意到它现在链接到他写的一本在线书。。。