‘of’ vs ‘from’ operator

It is important to note the difference between of and from when passing an array-like structure (including strings): Observable.of([1, 2, 3]).subscribe(x => console.log(x)); would print the whole array at once. On the other hand, Observable.from([1, 2, 3]).subscribe(x => console.log(x)); prints the elements 1 by 1. For strings the behaviour is the same, but at character … Read more

RxJS Observables nested subscriptions?

As mentioned in comments, you are looking for the flatMap operator. You can find more details in previous answers : How to do the chain sequence in rxjs Why do we need to use flatMap? Your example would read as : this.returnsObservable1(…) .flatMap(success => this.returnsObservable2(…)) .flatMap(success => this.returnsObservable3(…)) .subscribe(success => {(…)});

Difference between .unsubscribe to .take(1)

Each serves a different purpose so it’s hard to compare them. In general if you take this source: const source = range(1,3); … and consume it with subscribe() followed immediately by unsubscribe(): source.subscribe( console.log, undefined, () => console.log(‘complete’) ).unsubscribe(); … then all values from source are going to be emitted even though we called unsubscribe() … Read more

How to force observables to execute in sequence?

If you want to be sure the order of emissions is the same as the order in which you specified the source Observables you can use concat or concatMap operators. The concat* operators subscribe to an Observable only after the previous Observable completes (it works with Promises as well, see http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObservableInputDoc.html). In you case it’d … Read more

When to use asObservable() in rxjs?

When to use Subject.prototype.asObservable() The purpose of this is to prevent leaking the “observer side” of the Subject out of an API. Basically to prevent a leaky abstraction when you don’t want people to be able to “next” into the resulting observable. Example (NOTE: This really isn’t how you should make a data source like … Read more