Reputation: 568
I am trying to create an observable which will emit delayed values. However the values are getting instataneously. I have created a stackblitz at https://stackblitz.com/edit/typescript-cwixas. The code is given below:
import { of } from 'rxjs';
import { map, delay } from 'rxjs/operators';
const arr = [1,2,3,4,5];
const source = of(...arr);
const source2 = source.pipe(delay(2000));
const subscribe = source2.subscribe(val => console.log(val));
Upvotes: 2
Views: 122
Reputation: 96959
The delay()
operator just adds delay to each next
value but it doesn't care whether the previous delay elapsed.
So what happens here is that of()
immediately emits 5 values where all 5 are scheduled to be re-emitted by delay
at the same time after 2s.
You'll have to use for example concatMap
that will project each value into an Observable and wait until that completes. Only then continue with another value:
const arr = [1,2,3,4,5];
const source = of(...arr).pipe(
concatMap(value => of(value).pipe(
delay(2000),
)),
);
source.subscribe(...);
Your update demo: https://stackblitz.com/edit/typescript-cknhbr?file=index.ts
Upvotes: 2