how to use the of function from the rxjs library in typescript

To use the of operator from the rxjs library in Typescript, first you need to import it as follows:

index.ts
import { of } from 'rxjs';
27 chars
2 lines

Then, you can create an observable using the of operator:

index.ts
const observable$ = of('hello', 'world', 2022);
48 chars
2 lines

The of operator will emit each of the provided values as separate emissions in the observable stream.

Here's an example that subscribes to observable$ and logs each emitted value to the console:

index.ts
observable$.subscribe(val => console.log(val));
48 chars
2 lines

Output:

index.ts
hello
world
2022
17 chars
4 lines

Note: the $ suffix appended to the observable variable name is a convention in RxJS for indicating that the variable holds an observable.

gistlibby LogSnag