Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to execute code after concat operator finish

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

You should use finalize instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading