mituri

[RXJS] takUntil

Feb 25th, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /*
  2. Including https://unpkg.com/@reactivex/[email protected]/dist/global/rxjs.umd.js
  3. in index.html's script tag gives us rxjs globally available under window.rxj
  4. */
  5. const { Observable } = rxjs
  6. const { tap, map, first, takeUntil } = rxjs.operators
  7.  
  8. const observable$ = Observable.create((observer) => {
  9. observer.next(1)
  10. observer.next(2)
  11. observer.next(3)
  12. setTimeout(() => {
  13. observer.next(4)
  14. observer.complete()
  15. }, 4000)
  16. })
  17.  
  18. const observableTakeUntil$ = Observable.create((observer) => {
  19. setTimeout(() => {
  20. observer.next()
  21. observer.complete()
  22. }, 2000)
  23. })
  24.  
  25. observable$
  26. .pipe(
  27. map(data => data * 10),
  28. takeUntil(observableTakeUntil$)
  29. )
  30. .subscribe((data) => {
  31. console.log(data) // 1, 2, 3, 4
  32. })
Add Comment
Please, Sign In to add comment