Observable emit only one value using Delay within ConcatMap

大城市里の小女人 提交于 2020-07-22 06:03:14

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!