I have an array of observables that must be executed in order, but I need to return the set of observables cold (without subscribing) so that additional chaining can be performed.
const work = [observable1, observable2, observable3];
if I return concat(...work)
and something attempts to chain from it work$.pipe(switchMap(() => ...))
, the switchMap will be executed for each observable inside of concat - 3 times.
I'm looking for something similar to forkJoin where forkJoin(...work).pipe(switchMap(() => ...))
where the switchMap will only be executed once. But I can't just use forkJoin since the order must also be preserved.
The options I've found so far are:
- Return
forkJoin(concat(...work))
technically works since the forkJoin will wait for the single concat to complete, but it also feels wrong since I'm only ever feeding forkJoin a single observable just for the side effect of it waiting for it to complete when it was made specifically to handle multiple observables. - Return
concat(...work).pipe(toArray())
maybe better than forkJoin but still feels like I'm missing a better operator. There is alsotakeLast()
which functions roughly the same but still feels wrong since it seems to imply I care about the last value when I just want the completion. Almost something like finalize() but one that emits so it can be used to block the stream.
Is there a better operator for waiting for concat to complete, or just a better alternative to concat altogether that will execute the observables in order and let me chain the result like forkJoin does? Just feels like with the existence of forkJoin they have operators for waiting until multiple observables finish, and they have operators for maintaining order, there must be a basic one I'm missing that just waits for a source observable to complete and then emits.