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

Angular rxjs how to call multiple same http requests like one subscription?

For example I have multiple ids : [1,2,3,4,5] and then for each id I want to call delete api endpoint.Normally I would have to call it 5 times with forEach function :

ids.forEach(item => {
this.myService.delete(item).subscribe.....

}) 

But what if I want to combine it to one observable, so I could know when the loop has ended (complete state in rxjs) ?

Can I do something like this ?

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 :

You can first create the array of observables, then use forkJoin to execute them parallely!

deleteIds(ids: Array<number>) {
    // create a array of delete observables to be executed at once
    const deleteIds = ids.map(id => this.myService.delete(id));
    forkJoin(deleteIds).subscribe(() => {
        // execute the rest of the code
    });
}
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