create

signature: create(subscribe: function)

Example 1: Observable that emits multiple values
Example 2: Observable that emits even numbers on timer
  1. import { Observable } from 'rxjs/Observable';
  2. /*
  3. */
  4. const evenNumbers = Observable.create(function(observer) {
  5. const interval = setInterval(() => {
  6. if (value % 2 === 0) {
  7. observer.next(value);
  8. }
  9. value++;
  10. }, 1000);
  11. });
  12. //output: 0...2...4...6...8
  13. const subscribe = evenNumbers.subscribe(val => console.log(val));
  14. //unsubscribe after 10 seconds
  15. setTimeout(() => {
  16. subscribe.unsubscribe();
  17. }, 10000);

Additional Resources

  • create
    :newspaper: - Official docs

  • :dollar: - Shane Osbourne