问题
Having the following code:
range(1, 30)
.pipe(
windowCount(10),
concatMap(x => x.pipe(delay(5000))),
)
.subscribe(console.log);
For some reason only the first value is emitted (1..10), Could someone point out what's wrong with the above code? Thanks.
expected output: 1..10 (delay) 11..20 (delay) and so on....
回答1:
This is happening because windowCount
will complete the previous inner "window" before creating a new one. You delay each "window" by 5s but when concatMap
wants to subscribe to the next "window" it has completed already and will never ever emit anything.
Note, that windowCount
will emit all windows asap regardless whether concatMap
had even chance to subscribe to them.
回答2:
I ended up using bufferCount and expected output is achieved.
range(1, 30)
.pipe(
bufferCount(10),
concatMap(x => x.pipe(delay(5000))),
)
.subscribe(console.log);
DEMO
来源:https://stackoverflow.com/questions/62813365/observable-emit-only-one-value-using-delay-within-concatmap