Home > database >  Angular how to await for one subscribe to continue
Angular how to await for one subscribe to continue

Time:05-26

How can I await for a subscribe to continue my code sequence?

My subscribe is into a function, but I just want that this function finish if this subscribe execute.

My Subscribe:

this.massService.getMass(token, userId).subscribe((data: IMass) => {
    // Execution
})

My Service

getMass(token: string, userId: string): Observable<IMass> {
    const httpParams = new HttpParams().set('userId', userId);

    const options = {
        params: httpParams,
        headers: new HttpHeaders().set('Authorization', 'Bearer '   token)
    };
    return this.http.get<IMass>(environment.apiUrl   '/mass', options);
}

CodePudding user response:

You can convert observable to promise like :-

this.massService.getMass(token, userId).pipe(take(1),tap((res) => //execution)).toPromise();

And use await like you use with promise.

  • Related