how to make second stream work before first rxjs

this.productService.createProduct(form).subscribe((res) => {
  this.fileUploadService.remove(imageName).subscribe((res) => {
    this.closeDialog(true);
  })
})

I have this snipped from my code and how you can see, I’m subscribing to second request inside first one, I think this is a bad solution, do you know how to make the second stream work before first one, by using rxjs operators?

>Solution :

If the this.fileUploadService.remove has to be executed before this.productService.createProduct then you can try

this.fileUploadService.remove(imageName).pipe(
  concatMap(() => this.productService.createProduct(form))
).subscribe(() => {
    this.closeDialog(true);
})

The key here is the use of concatMap which basically says "execute downstream after the upstream has notified some values".

Leave a Reply