gpt4 book ai didi

javascript - rxjs 只在第一次执行tap

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:09:10 26 4
gpt4 key购买 nike

我只想在获得第一个发射值时才执行 tap()

类似于:

Observable
.pipe(
tap(() => { /* execute only when I get the first emitted value */ })
)
.subscribe(() => {
// .....
})

最佳答案

您可以在 concatMap 等 map 运算符中使用索引。与其他方法不同,这对于所选索引是完全灵活的。假设您想要点击第二次发射 index === 1 或任何谓词,例如 index % 2 === 0

// these are because of using rxjs from CDN in code snippet, ignore them
const {of, interval} = rxjs;
const {take, tap, concatMap} = rxjs.operators;


// main code
const stream = interval(250).pipe(take(4))

stream.pipe(
concatMap((value, index) => index === 0
? of(value).pipe(
tap(() => console.log('tap'))
)
: of(value)
)
)
.subscribe(x => console.log(x));
<script src="https://unpkg.com/@reactivex/rxjs@6.x/dist/global/rxjs.umd.js"></script>

关于javascript - rxjs 只在第一次执行tap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54099238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com