Home > Back-end >  How to execute code after concat operator finish
How to execute code after concat operator finish

Time:01-10

I have tasks (observables) in array and want run them sequentially and at the end I want to run some finish code

const { concat, of, tap } = rxjs;
const { delay  } = rxjs.operators;

let tasks = [
  of(1).pipe(delay(1000)),
  of(2).pipe(delay(2000)),
  of(3).pipe(delay(1000)),
  of(4).pipe(delay(2000)),
];

// In below example, word "Finish" is printed 4 times 
// but I want print it only once, at the end.

concat(...tasks)
  .pipe(tap(()=> console.log('Finish')))      
  .subscribe(n => console.log(`result for task ${n}`));
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.8.0/rxjs.umd.min.js" integrity="sha512-v0/YVjBcbjLN6scjmmJN h86koeB7JhY4/2YeyA5l rTdtKLv0VbDBNJ32rxJpsaW1QGMd1Z16lsLOSGI38Rbg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

I try to use tap but without success - how to do it?

CodePudding user response:

Tap, how you use, will run on each emited value.

You should use finalize instead.

  • Related