angular how to await subscribe

you can achieve this using the firstValueFrom method. It is also a great way to handle asynchronous operations. Just remove the subscribe and add this method and add the async keyword in the method from where you are calling this method.

Example-

 const response = await firstValueFrom(this._roleService.getRoleTypes(this.token));

So, Now you will get console.log(“A”) first then console.log(“B”).

UPDATE: firstValueFrom is only available from RxJS v7 onwards. If you are using RxJS v6 or less, you will need to use .ToPromise(). So:

 const response = await this._roleService.getRoleTypes(this.token).ToPromise();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)