rxjs see if observable has emitted in typescript

To determine if an observable has emitted a value, you can use the first() operator along with the subscribe() method.

Here is an example code block using TypeScript:

index.ts
import { Observable } from 'rxjs';
import { first } from 'rxjs/operators';

const myObservable: Observable<number> = // create your observable here

myObservable.pipe(
  first()
).subscribe(
  () => console.log('Observable has emitted a value'),
  () => console.log('Observable has not emitted a value')
);
307 chars
12 lines

In this example, the first() operator retrieves the first value emitted by the observable and then the subscribe() method handles the callbacks: the first one is called when the observable emits a value and the second one when it completes without emitting. You can replace the console log statements with any other functionality you want to implement.

gistlibby LogSnag