Reputation: 131
Given an array of observables where each observable can emit EMPTY, how can I execute the next observable in the array on the condition that the previous observable returned EMPTY or stop iterating the array and return a non EMPTY value once an observable in the array has emitted the first non EMPTY value?
Upvotes: 1
Views: 267
Reputation: 11924
Here would be one approach:
const emptyVal = Symbol('empty');
const src$ = concat(
...arrOfObservable.map(
obs$ => obs$.pipe(
last(null, emptyVal)
)
)
).pipe(
filter(v => v !== emptyVal),
first()
)
The last()
operator will emit the last emitted value, after the source completes. If there is no last value(obs$
has used EMPTY
), it will emit emptyVal
.
filter(v => v !== emptyVal)
will make sure that we keep iterating until we get a non empty value.
With first()
, we'd stop the iteration and we'd get the emitted value.
Note: this approach works if the obs$
completes at some time, otherwise last
won't be able to do its job.
Upvotes: 1