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;
});