rxjs execute tap only at the first time

前端 未结 6 1005
臣服心动
臣服心动 2020-12-19 02:18

I want to execute tap() only when i get the first emitted value

Something like:

Observable
  .pipe(
     tap(() => { /* execute only when I get th         


        
6条回答
  •  囚心锁ツ
    2020-12-19 03:00

    (Updating my earlier incorrect answer)

    Based on cartant's comment and link provided, he has already done the work to create an operator that does this, it is in the 'rxjs-etc' package. A solution based on his operator is to install 'rxjs-etc', then:

    import { initial } from 'rxjs-etc/operators';
    
    observable$.pipe(
        initial(src$ => src$.pipe(tap(() => {/* execute only on first value */})))
    ).
    subscribe(() => {
        // ..... 
    })
    

    Working StackBlitz example.

提交回复
热议问题