您可以使用CombineLatest返回的事件,你想,例如:
/* Have staggering intervals */
var source1 = Rx.Observable.interval(1000)
.map(function(i) {
return {
data: (i + 1),
time: new Date()
};
});
var source2 = Rx.Observable.interval(3000).startWith(-1)
.map(function(i) {
return {
data: (i + 1),
time: new Date()
};
});
// Combine latest of source1 and source2 whenever either gives a value with a selector
var source = Rx.Observable.combineLatest(
source1,
source2,
function(s1, s2) {
if (s1.time > s2.time) {
return s1.data + ', ' + s2.data;
}
return undefined;
}
).filter(x => x !== undefined).take(10);
var subscription = source.subscribe(
function(x) {
console.log('Next: %s', x);
},
function(err) {
console.log('Error: %s', err);
},
function() {
console.log('Completed');
});
這是一個很好的問題!我將看看這個[RxJS 5運營商的示例:combineLatest](https://gist.github.com/btroncone/d6cf141d6f2c00dc6b35#combinelatest),看看我能不能拿出一些東西。 – wolfhoundjesse