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();