RxJS/Observable flatMap can return Observable or array
Asked Answered
G

1

9

Can someone explain to me why the .flatMap operator can accept a function which returns an Observable, or an array?

The official docs say:

The FlatMap operator transforms an Observable by applying a function that you specify to each item emitted by the source Observable, where that function returns an Observable that itself emits items.

Why can it also return an array?

For example, these are both valid:

obs$.flatMap((data) => {
    return [];
});

obs$.flatMap((data) => {
    return new Observable<string>();
});

But this does not work:

obs$.flatMap((data) => {
    return 1;
});
Gaut answered 22/7, 2017 at 23:59 Comment(5)
Observables can be thought of as arrays spread over time, so flattening an array is like flattening a synchronous observable. 1 is neither an array nor an observable, so of course it doesn't work.Alika
flatMap is an alias for mergeMap. Check the docs and you will see the function can return an observable, a promise, or an array-like value: reactivex.io/rxjs/class/es6/…Alika
Specifically, it can return an ObservableInput: reactivex.io/rxjs/class/es6/…Alika
Thanks, that helped my understanding of it a lot, did not realise it was ObservableInput. So how can .flatMap return an array of Observables? Or is that not possible?Gaut
If you call flatMap on an array of observables, you'll get an observable of observables - termed a higher-order observable. There are operators to further flatten such higher-order observables. Look at mergeAll and concatAll. Also, if the links have helped you understand things, you might want to consider adding a self answer.Alika
S
7

The official docs are not relevant because they refer to RxJS 4 and not RxJS 5.

mergeMap projection function returns not just Observable but ObservableInput interface, which applies to miscellaneous values that can be converted to observables:

Arrays can be interpreted as observables that emit all values in array one by one, from left to right, and then complete immediately.

This means that

obs$.flatMap((data) => arr)

is basically a shorter version of

obs$.flatMap((data) => Observable.from(arr))
Stronski answered 23/7, 2017 at 1:16 Comment(1)
Thanks! That helped me understand it much moreGaut

© 2022 - 2024 — McMap. All rights reserved.