Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. **This may not be the answer to your question but it is an imporovment/fixing a future bug(?) to your first method.**
  2.  
  3. **The problems:**
  4.  
  5. 1. You subscribe inside the promise but don't unsubsribe after so you will get unexpected behavior after the promise resolved successfully.
  6. 2. I guess you are expecting the subsription at the first time will happen befor the promise resolved but it may not be the case here. You need to insure that by your self.
  7.  
  8. **The solution:**
  9.  
  10. usign the `take(1)` and `toPromise()` methods of rxjs:
  11. 1. `take(1)` - if `this.usersService.checkEmail(control.value)` will never complete (maby a firebase subscription), use `take(1)` to inforce it to complete.
  12.  
  13. 2. `toPromise()` - "cast" it to promise so you can return what u need and most importent, insure that what is inside happens **befor** the promise resolved.
  14.  
  15. Note: If you won't add `take(1)` but only `toPromise()`, your promise will never be resolved and what inside will never be called incase the observable will never complete.
  16.  
  17. **Code:**
  18.  
  19. emailTaken(control: Control): Promise<ValidationResult> {
  20. return this.usersService.checkEmail(control.value)
  21. .take(1)
  22. .toPromise()
  23. .then(res=>....);
  24. }
  25.  
  26. -----------------
  27. *I hope I'm wrong and this was the behavior you wanted to get in the first place. Incase you didn't here is a better way.
  28. There is no need to accept this as an answer.*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement