import { EventEmitter } from 'events' ; import { empty , fromEvent , fromEventPattern , merge , never , throwError } from 'rxjs' ; import { mergeMap } from 'rxjs/operators' ; const emitter1 = new EventEmitter ( ) ; const emitter1$ = fromEvent ( emitter1 , 'data' ) ; emitter1 . emit ( 'data' , 'hello world' ) ; const emitter = new EventEmitter ( ) ; const data$ = fromEvent ( emitter , 'data' ) ; const error$ = fromEvent ( emitter , 'error' ) . pipe ( mergeMap ( err => throwError ( err ) ) ) ; const merged$ = merge ( data$ , error$ ) ; merged$ . subscribe ( { next : x => console . log ( `Data: ${ x }` ) , error : x => console . log ( `Error: ${ x }` ) } ) ; emitter . emit ( 'data' , 'foo' ) ; emitter . emit ( 'error' , new Error ( 'woops' ) ) ; emitter . emit ( 'data' , 'bar' ) ;