RXJS zip of zip (arrays) observable is not firing

2986 views rxjs
5

I am trying to get result from a zipped array of observables themselves zips of array of simple observables. As follows:

a(x) {
    const observables = [of(x), of(x+1)];
    return zip(observables);
}

b() {
    const observables = [a(1), a(2)];
    return zip(observables);
}

The rest of the code is asserted to work fine. Actually, when the inner a() function returns a single observable (of array of objects of course, to reflect the zip of observables) the outer zip works fine. Yet when an inner zip is used, the code inside the inner zip is never called.

What am I doing wrong here?

answered question

1 Answer

9

How many hours have I spent with this issue..

Try:

zip(...observables);

The parameter has to be spreaded, otherwise your array will be treated as ObservableInput, which is probably not the desired behaviour.

You can include this function as a safe fallback for your use-case:

export function zipArray<T>(array: ObservableInput<T>[]): Observable<T[]> {
    return array.length ? zip(...array) : of([]);
}

And call it like this:

zipArray(observables)

posted this

Have an answer?

JD

Please login first before posting an answer.