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.
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]);
This as been fixed.
@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?
@blesh was a static operator added? The other closed issue referred back to this one. :)
@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.
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.
Same issue as described by @jdelaune and @oka-garaio.
That being said, adding the ... before the array variable name being passed into merge as mentioned by @blesh does make it work.
Rx.Observable.merge(...[1,2,3]) // (?)
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.
Same problem in the context of an Ionic 2 app utilising Observables.
This subsection of a larger observable chain breaks:
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));
TypeError: __WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
.flatMap is not a function. (In '__WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
.flatMap(function (test) { return test; })', '__WEBPACK_IMPORTED_MODULE_1_rxjs_Observable__["Observable"].merge(test)
.flatMap' is undefined)
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));
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".
I'm currently experiencing the same issue...Though, my complete() method also does not fire.
I spent some tim trying to make this work, only to find out I had to add the .... Thanks a lot to the commenters on this thread for the solution. I wish the feedback will be consider here because this API is not easy to use as-is.
Is there any reason not to have a version of .merge that takes an array? Cause this seems like a simple PR to make, but I'd like to know if there's a reason not to do it before going ahead.
@samal84 I think that's because merge internally comes down to subscribeToResult which means you can use it with arrays of items as well. For example you can write this:
Observable.zip(
Observable.interval(1000),
Observable.merge([1, 2, 3, 4, 5])
).subscribe(console.log);
The merge operator takes the array and emits each number as a separate item.
So if you wanted merge to accept an array of Observables it can't know whether you want to subscribe to each one of them or emit them as Observables (higher-order Observable).
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
mergedoesn't currently accept an array, it only acceptsrestargs. The work around is to use the...operator.and