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?
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))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With