LiveLoveApp logo

Solution - Observables

Solution

import { fromEvent } from 'rxjs';

const observable = fromEvent(document, 'click');
observable.subscribe(() => {
  console.log('Hello from Observableland!');
});

See the solution on codesandbox

Let's review the solution code above:

  • First, we import the fromEvent() function from the rxjs dependency.
  • Second, we invoke fromEvent(), specifying the document as the target and click as the event name.
  • The fromEvent() function returns a new Observable instance.
  • We invoke the subscribe() method on the Observable.
  • The subscribe() method accepts a callback function that is invoked when the Observable produces a value.
  • In this example, we are logging out a message to the console.