how to use the endwith function from the rxjs library in javascript

To use the endWith operator from the rxjs library in JavaScript, first, we need to import it:

index.tsx
import { of } from 'rxjs';
import { endWith } from 'rxjs/operators';
69 chars
3 lines

Then, we can create an Observable using the of operator, and use the endWith operator to append a sequence of values to the end of the Observable:

index.tsx
const observable = of(1, 2, 3).pipe(
  endWith(4, 5, 6)
);

observable.subscribe(val => console.log(val));
// Output: 1, 2, 3, 4, 5, 6
135 chars
7 lines

In the above example, the endWith operator appends the values 4, 5, and 6 to the end of the Observable, and the subscribe method logs all the emitted values to the console.

gistlibby LogSnag