Reactive-Extensions / RxJS

The Reactive Extensions for JavaScript
http://reactivex.io
Other
19.48k stars 2.1k forks source link

throttle example wrong? #1384

Open Kenzku opened 7 years ago

Kenzku commented 7 years ago

The example in the throttle the example output is: 0,2,3 but if I copy the code to jsbin, and with 4.0.6 version RxJS I got result: 0,1,3

it just feels like the time interval between 0 to 4, and 4 to 2 are less than 300ms, so they are gone. screen shot 2016-12-07 at 14 09 33

Example

var times = [
  { value: 0, time: 100 },
  { value: 1, time: 600 },
  { value: 2, time: 400 },
  { value: 3, time: 900 },
  { value: 4, time: 200 }
];

// Delay each item by time and project value;
var source = Rx.Observable.from(times)
  .flatMap(function (item) {
    return Rx.Observable
      .of(item.value)
      .delay(item.time);
  })
  .throttle(300 /* ms */);

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

// => Next: 0
// => Next: 2
// => Next: 3
// => Completed