0

Scenario:

I developed a service that validates signatures (OtpSMS and Position). This service has two main functions:

--> validateSignPosition() : Observable --> validateSignOtpSMS() : Observable

Two functions mentioned are asynchronous because need to demand a challenge to remote server. There is the reason because I used observables to sustain the correct outpour of application.

Problem

I have a restriction about not being able to use ngOnDestroy to unsuscribe this observables. Then, I was thinking about realize unsuscribe action when I recieve data or an error exception. Like this example:

currentObserver = this.service.validateSignOTP().subscribe(
   data => { this.currentObserver.unsubscribe() },
   error => { this.currentObserver.unsubscribe() }
...

I believe there are another option to not replicate code. I heard about finally/onComplete section which executes although recieves data or exception. But, I do not know if this works with asynchronous outpour...

Do you know how can unsuscribe observables to this scenario?

Thank you.

JoseJimRin
  • 372
  • 1
  • 4
  • 20
  • 1
    Have a look here https://stackoverflow.com/questions/28007777/rxjs-create-subscribe-once-and-dispose-method – David Feb 19 '18 at 09:53
  • 1
    You might also use `takeUntil` and a condition. Take a look here https://medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87 – nicowernli Feb 19 '18 at 10:00
  • Well, if I use first() and unsubscribe works. But I am forced to use both because if only use one of them it does not works. Is necessary to use both? Or, theoretically, I should only use first() for this scenario. Thanks! @David – JoseJimRin Feb 21 '18 at 10:58

0 Answers0