First Value From
To avoid the use of subscribe and unsubscribe, we can use the firstValueFrom
function from rxjs
to get the first value from an observable.
ts
import { firstValueFrom } from 'rxjs';
async submit() {
const _o$ = this.modifyLocation.mutate({
...
})
.pipe(
tap((data) => {
...
}
));
await firstValueFrom(_o$);
}
Or
ts
@Component({
...
})
export class MyComponent {
private destroyRef = inject(DestroyRef)
constructor(){
interval(1000)
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe(res => console.log(res))
}
}