【发布时间】:2022-01-16 21:37:42
【问题描述】:
我希望构建一个 rxjs 管道,仅当当前事件已通过整个管道(FIFO 结构)时才会发出下一个事件。
const { from } = rxjs;
const { map, filter } = rxjs.operators;
// the event `2` should only be emitted
// when the event `1` has reached the end of the pipeline.
// and so on and so fort
from([1, 2, 3, 4, 5, 6, 7]).pipe(
map((n) => n ** 2),
filter((n) => n % 2),
).subscribe(console.log);
/**
-1-------|
-2-------|-1
...
**/
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.4.0/rxjs.umd.js" integrity="sha512-DRDXreq+zyiPhlKTfJ5pgzWRn+6SgJ7cPoRxMNksyHmUEOjKiKIoqvssNYNwknpvVbQsVN5hlhh3sp0rxbU7Bg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
【问题讨论】:
-
在我看来,在您的示例中,顺便说一句,它是完全同步的,这正是发生的情况:只有在 1 在订阅提供的函数中通知后,2 才进入管道。如果事情是异步的,那么
exhaustMap运算符可能会有所帮助。我错过了什么吗?
标签: javascript rxjs queue rxjs6