I was originally searching how to return several actions in a ngrx effect, and found I need to return an array of actions.
Then I noticed that returning a simple array in the switchMap works as fine as returning an observable created from this array.
For example:
timer(1000).pipe(switchMap(val => from([val, val + 1)])).subscribe(val => console.log(val));
timer(1000).pipe(switchMap(val => [val, val + 1])).subscribe(val => console.log(val));
I expect the first to work and think it is the correct syntax.
I don't expect the second to work but it actually does and I would like to understand why.
Thanks,
Because switchMap
, among other flattening-operators (mergeMap
, exhaustMap
, ...), takes an ObservableLike
as the return type of its projection function.
An ObservableLike
can be Observable
, Promise
or Array
. If you provide an array, it is converted into a stream of its items - basically the same as if you had used from
.