RxJS Functions (Part 8)

Corey Pyle
2 min readJan 15, 2019

--

fromEvent

fromEvent

Creates an Observable that emits events of a specific type coming from the given event target.

TL;DR

Make an Observable from almost any kind of event (e.g. a click, a WebSocket event, a node event, etc.).

Use Case

An interesting feature of fromEvent is its ability to take in NodeList and HTMLCollection. Let’s take a look at how you might make an Observable, without fromEvent that emits for the same event on multiple elements:

A little verbose, no? Now let’s use fromEvent:

Now, granted that example two could be refactored a bit, this is still much simpler to read. Consider this capability and that of tying fromEvent to something like WebSockets, and there’s no doubt the fromEvent is one of the more convenient RxJS functions.

Thanks for reading. Stay tuned for Part 9, and be sure to check out my previous articles.

--

--