RxJS和-thenDo-例子

时间:2016-07-26 16:35:13

标签: operators rxjs5

你能为我做一个有效的例子吗?哪个用例可以/应该应用这种模式?这里的信息:http://reactivex.io/documentation/operators/and-then-when.html#collapseRxJS很短。

1 个答案:

答案 0 :(得分:0)

以下是RxJS 4文档的示例: 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