Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to produce value every 1 sec on rxjs

If I already have an observable, then what operator should I use to make this observable to produce value like, every 1 sec?

// this just an example, In my project, I can't control when the 
// observable will produce value. I can assume that it will produce
// value very fast.
const obs = from([1,2,3,4,5]);

The obs will emit the value 1,2,3... very quickly. But what if I want it to emit the value every 1 sec? I mean, just make sure that obs emit value not too quick?

I checked the document from reactivex, and can't find a operator to do so. For example, delay, which just make the value production delay some time, but the relative time intervals between the values are preserved, and debounceTime do produce value periodically but ignore values at that time window.

Can someone tell me how to make the observable produce value on period time and not miss or ignore values?

like image 219
zeyang yue Avatar asked Nov 17 '25 16:11

zeyang yue


1 Answers

You could zip it with an interval observable like this:

import { zip, from, interval } from 'rxjs'

const obs = zip(
  from([1,2,3,4,5]),
  interval(1000),
  (val, i) => val // Just emit the value
)

obs.subscribe(val => console.log(val))

If you want the first value to emit immediately then you could use timer instead of interval:

import { zip, from, timer } from 'rxjs'

const obs = zip(
  from([1,2,3,4,5]),
  timer(0, 1000),
  (val, i) => val // Just emit the value
)

obs.subscribe(val => console.log(val))

You can also use a pipe if you prefer, like this:

import { from, interval } from 'rxjs'
import { zip } from 'rxjs/operators'

const obs = from([1,2,3,4,5])
  .pipe(
    zip(interval(1000), val => val)
  )

obs.subscribe(val => console.log(val))

Update

The zip operator has been replaced with zipWith, which has no resultSelector parameter. zip Will be removed in v8.

Therefore, the example above that uses the zip operator can be updated as follows:

import { from, interval } from 'rxjs'
import { map, zipWith } from 'rxjs/operators'

const obs = from([1,2,3,4,5])
  .pipe(
    zipWith(interval(1000)),
    map(val => val[0])
  )

obs.subscribe(val => console.log(val))
like image 154
Steve Holgado Avatar answered Nov 19 '25 06:11

Steve Holgado