LiveLoveApp logo

Solution - New Observable

Solution

import { Observable } from 'rxjs';

const o = new Observable((subscriber) => {
  subscriber.next(1);
});
o.subscribe((value) => {
  console.log(value);
});

See the solution on codesandbox

Let's review the solution code above:

  • First, we import the Observable class from the rxjs dependency.
  • Next, we create a new instance of the Observable class.
  • We provide a callback function that is invoked with the subscriber.
  • We invoke the next() method on the subscriber, specifying the value of 1.
  • We invoke the subscribe() method on the Observable instance and log out the value.