Search code examples
rxjsrxjs5

Unsubscribe first observable in switchmap


I have two observables, the second need the result of the first, so I used switchMap :

this.fooSharer.getFoo().pipe(switchMap((foo: Foo) => {
    return this.http.post('/someUrl',
    body,
    headers
  )
}));

So I have in return an Observable of Object (from http.post), but my subscription from the foo Observale is still active and I can't unsubscribe, how can I do ?


Solution

  • If you use emitted value from fooSharer.getFoo() just as a signal to start the second Observable you can put take(1) just before the switchMap. This way, after the first emitted value the fooSharer.getFoo() will complete.