How to convert an Observable to a ReplaySubject?

You can use just observable.subscribe(subject) if you want to pass all 3 types of notifications because a Subject already behaves like an observer. For example: let subject = new ReplaySubject(); subject.subscribe( val => console.log(val), undefined, () => console.log(‘completed’) ); Observable .interval(500) .take(5) .subscribe(subject); setTimeout(() => { subject.next(‘Hello’); }, 1000) See live demo: https://jsbin.com/bayewo/2/edit?js,console However this … Read more

Rxjs Observable.take(1) vs Subscription.unsubscribe()

The take(1) approach has a number of advantages over subscribe: Code readability (and elegance). The second approach requires that you hold and manage extra variables. The second approach will not invoke the complete handler. This is because .take(1) actually create a new observable which potentially yields a single item and completes. The second approach will … Read more

Observables: Complete vs finally vs done

Finally always happens whenever an observable sequence terminates (including errors); completed only happens when it terminates without errors. Finally: Invokes a specified action after the source observable sequence terminates gracefully or exceptionally. https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/finally.md OnCompleted: An Observable calls this method after it has called onNext for the final time, if it has not encountered any errors. … Read more

RxJS emit array items over time?

Three ways to do it, with RxJS version 6 : 1. Using concatMap import { from, of, pipe } from ‘rxjs’; import { concatMap, delay } from ‘rxjs/operators’; const array = [1, 2, 3, 4, 5]; from(array) .pipe( concatMap(val => of(val).pipe(delay(1000))), ) .subscribe(console.log); 2. Using zip and interval import { from, pipe, interval } from … Read more

Testing Observables with jest

There are some good examples in the Jest documentation about passing in an argument for the test. This argument can be called to signal a passing test or you can call fail on it to fail the test, or it can timeout and fail. https://jestjs.io/docs/en/asynchronous.html https://alligator.io/testing/asynchronous-testing-jest/ Examples Notice I set the timeout to 1500ms const … Read more

RxJS, understanding defer

Quite simply, because Observables can encapsulate many different types of sources and those sources don’t necessarily have to obey that interface. Some like Promises always attempt to eagerly compete. Consider: var promise = $.get(‘https://www.google.com’); The promise in this case is already executing before any handlers have been connected. If we want this to act more … Read more

Rxjs conditional switchMap based on a condition

Another possibility is to use the operator iif in a SwitchMap. https://www.learnrxjs.io/learn-rxjs/operators/conditional/iif https://rxjs-dev.firebaseapp.com/api/index/function/iif but it can restrain the possibility to control specfic conditions on your Observable : myObservable1 .pipe( switchMap(result1 => iif(() => condition , myObservable2 , myObservable1 ) ) .subscribe(result => console.log(result)); Where you can replace ‘condition’ by a function returning a boolean. With … Read more

Can’t find `combineLatest` in RxJS 5.0

I think #1722 is the relevant GitHub issue here. I’m on a project using typescript@2.0.10, RxJS@5.0.3, and webpack@2.1.0-beta.25. The following works for me: import { Observable } from ‘rxjs/Observable’; import ‘rxjs/add/observable/combineLatest’; Observable.combineLatest( source1, source2 ).subscribe(sink);

What is the difference between throttleTime vs debounceTime in RxJS and when to choose which?

I think in your case throttleTime works a little bit better, because you want to make the api request as soon as user clicks the button. Both throttleTime and debounceTime ignore the events which come in the meantime, but throttleTime emits right away, while debounceTime waits for additional delay. You can visually see that very … Read more

tech