Rx.Observable.amb(...args)

amb

</rx-marbles>

从一系列流中,订阅最先发射的值的可观察对象并忽略其他的可观察对象。“amb”代表ambiguous

参数

  1. args (Array|arguments): 多个流(可观察源或Promises、数组)作为参数。

返回值

(Observable): 返回首先返回值的可观察对象

使用可观察序列
var source = Rx.Observable.amb(
    Rx.Observable.timer(500).select(() => 'foo'),
    Rx.Observable.timer(200).select(() => 'bar')
);

var subscription = source.subscribe(
  x => console.log(`onNext: ${x}`),
  e => console.log(`onError: ${e}`),
  () => console.log('onCompleted'));
// timer = 200先返回,所以只取第二个可观察对象的返回值
// => onNext: bar
// => onCompleted
混合使用Promises和Observables
var source = Rx.Observable.amb(
    RSVP.Promise.resolve('foo'),
    Rx.Observable.timer(200).select(() => 'bar')
);

var subscription = source.subscribe(
  x => console.log(`onNext: ${x}`),
  e => console.log(`onError: ${e}`),
  () => console.log('onCompleted'));
// 由于Promise是立即resolve,所以取Promise的返回值
// => onNext: foo
// => onCompleted

应用

由于只取最先返回值的可观察对象。所以可以应用于任何抢答、秒杀等一对多竞态应用。

results matching ""

    No results matching ""