RXJS: How can I generate a stream of numbers at random intervals (within a specified range)?

后端 未结 3 517
余生分开走
余生分开走 2020-12-16 23:29

I want to use RXJS to set up an ORDERED data stream that emits a number at a random interval (say every 1-5 seconds) which I want to use as a time-randomized data source for

3条回答
  •  心在旅途
    2020-12-17 00:17

    Use concatMap instead of flatMap.

    Documentation here: https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/concatmap.md

    var source = Rx.Observable
      .range(1, 10)
      .concatMap(function (x) {
        return Rx.Observable
          .of(x)
          .delay(randomDelay(1000,5000));
      })
     .timeInterval();
    

提交回复
热议问题