rxjs: Rx.Observable.merge is not accepting Array of Observables

Given:

const name$ = Rx.Observable.merge([
  Rx.Observable.of('Bruce Lee'),
  Rx.Observable.of('Brad Pitt'),
]);

Above code will return as a nested Observable. But if I remove the square bracket it works as expected.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 18 (6 by maintainers)

Most upvoted comments

merge doesn’t currently accept an array, it only accepts rest args. The work around is to use the ... operator.

 const Rx = require('rxjs');

   Rx.Observable.merge(...[
            Rx.Observable.timer(100),
            Rx.Observable.timer(2),
            Rx.Observable.timer(40)
        ])
       .subscribe();

and

let test: Observable<Order>[] = [];

for (let orderId of orderIds) {
    test.push(this.orderService.get(orderId)) //Produces Observables with an "order" in it.
}

return Observable.merge(...test)
   .flatMap(flat => flat)
   .toArray()
   .do(array => console.info('*ARRAY', array));

I think this is one of the cases for a static mergeAll which I’ve proposed elsewhere #786

If you’re using Babel or TypeScript, for now you can just do:

Rx.Observable.merge(...[observable1, observable2]);

Why was this closed and marked as fixed? @benlesh I’m using the ... operator as a workaround, and not really a big deal, but it isn’t “fixed”.

This as been fixed.

Rx.Observable.merge(...[1,2,3])  // (?)

I have the same issue here. I build up an array of observables which do, lets say install steps, then I merge them with Observable merge. And then I got the same behaviour.

I must be doing something wrong


  const Rx = require('rxjs');

   Rx.Observable.merge([
            Rx.Observable.timer(100),
            Rx.Observable.timer(2),
            Rx.Observable.timer(40)
        ])
       .subscribe();

I get subscribe is not a function. No idea what’s up with that. Anyone see the same? I also tried calling do() on the result of merge, same thing:

TypeError: Rx.Observable.merge(...).do is not a function

Weird.

@blesh I don’t think this has been fixed. In RC1 passing an array into merge still returns a nested Observable as stated in the original issue.

@blesh Not seeing the fix in beta10, also not as a function signature in the merge.d.ts. Can’t seem to find a linked commit… Am I overlooking something?

I’m currently experiencing the same issue…Though, my complete() method also does not fire.

That being said, adding the ... before the array variable name being passed into merge as mentioned by @blesh does make it work.

@blesh was a static operator added? The other closed issue referred back to this one. 😃