Fireship · @fireship

RxJS Quick Start with Practical Examples

Uploaded September 12, 2017 · Length 15:33

RxJS Quick Start with Practical Examples

Summary

This video teaches how to use RxJS with practical examples, covering observables, hot and cold observables, and various operators such as map, do, and filter.

  • rxjs
  • reactive programming
  • observables
  • rxjs operators
  • javascript

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    reactive X has revolutionized the way

  2. 0:02

    modern developers think about building

  3. 0:03

    apps it allows us to treat all data as a

  4. 0:06

    stream as it unfolds through the

  5. 0:07

    dimension of time in this lesson I'm

  6. 0:10

    going to cover about 25 different ways

  7. 0:11

    you can use our XJS so you have a solid

  8. 0:13

    foundation to use it in your own

  9. 0:15

    projects for this tutorial I've just

  10. 0:17

    created a basic HTML page and imported

  11. 0:20

    the rxjs script from a CDN in the head

  12. 0:22

    of that page then we'll write all of our

  13. 0:24

    custom code in this lesson J's file and

  14. 0:27

    import that in the body of the page then

  15. 0:29

    inside lesson Jas I'm creating a helper

  16. 0:31

    function that will just append the

  17. 0:33

    results from an observable to the

  18. 0:34

    browser on the right side here so when

  19. 0:36

    you see me use the print function it's

  20. 0:38

    to add the values to the browser here on

  21. 0:39

    the right the first thing we're going to

  22. 0:41

    talk about is the observable and I think

  23. 0:43

    the easiest way to think about it is

  24. 0:44

    just an array that gets built over time

  25. 0:47

    and you can loop over this array in the

  26. 0:49

    dimension of time by subscribing to it

  27. 0:51

    the first thing we're gonna do is create

  28. 0:53

    an observable from scratch

  29. 0:55

    we do this by calling our X dot

  30. 0:57

    observable create and then create takes

  31. 1:00

    a observer function where we can define

  32. 1:02

    what the observable sends to the

  33. 1:04

    subscriber to send values you call

  34. 1:07

    observer next with the value you want to

  35. 1:09

    send and that value can be anything it

  36. 1:10

    could be a string and object or whatever

  37. 1:12

    you want then to make the observable

  38. 1:15

    start emitting values you call subscribe

  39. 1:17

    on it in this case we'll take each value

  40. 1:18

    and just print it to the screen if we

  41. 1:21

    refresh our page you can see we get

  42. 1:23

    hello world print it to the screen so

  43. 1:25

    that's pretty cool but we don't normally

  44. 1:27

    need to create observables manually like

  45. 1:29

    this let's say we wanted to create an

  46. 1:31

    observable from click events in the Dom

  47. 1:33

    we can call observable from event pass

  48. 1:36

    at the document and the event we want to

  49. 1:38

    observe in this case clicks so just like

  50. 1:40

    before we subscribe to it but at this

  51. 1:42

    time we'll console.log it just to show

  52. 1:43

    the full event object each time we click

  53. 1:45

    the page we get the mouse event logged

  54. 1:47

    along with all of its associated data if

  55. 1:50

    you work with asynchronous JavaScript

  56. 1:51

    you're probably used to working with

  57. 1:52

    promises and we can convert a promise

  58. 1:55

    directly into an observable so here

  59. 1:57

    we're just creating a promise with a

  60. 1:59

    timeout that resolves after one second