import { Observable } from 'rxjs' ; let n = 0 ; let id = null ; const number$ = new Observable < number > ( observer => { id = setInterval ( ( ) => { if ( n ++ < 5 ) { observer . next ( n ) ; } else { observer . complete ( ) ; } } , 1000 ) ; return ( ) => clearInterval ( id ) ; } ) ; const observer = { next : ( x ) => { console . log ( `Next: ${ x }` ) ; } , complete : ( ) => { console . log ( `Complete!` ) ; } } ; const subscription = number$ . subscribe ( observer ) ;