Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS and-thenDo-when example

Dou you have a working example for me ? Which use-case can/should apply this pattern? The information here: http://reactivex.io/documentation/operators/and-then-when.html#collapseRxJS is very short.


1 Answers

Here is an example from the RxJS 4 docs: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/thendo.md

var selector = function (x, y) { return x + ", " + y; };

var source = Rx.Observable.when(
    Rx.Observable.interval(250).and(Rx.Observable.of("A", "B", "C")).thenDo(selector),
    Rx.Observable.interval(300).and(Rx.Observable.of("a", "b")).thenDo(selector)
);

var subscription = source.subscribe(
    function (x) {
        console.log('Next: ' + x);
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Next: 0, A 
// => Next: 0, a
// => Next: 1, B
// => Next: 1, b
// => Next: 2, C
// => Completed
like image 198
omerts Avatar answered Apr 03 '26 12:04

omerts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!