VISHAL DAGA
VISHAL DAGA

Reputation: 4289

Why is the code inside RxJS Observable constructor not being called?

If I do this in browser console, the constructor function doesnot seems to be called-

Observable.create(observer =>
   console.log('this is the observer'); //doesn't gets logged
   this.alertObserver = observer
);

or this :-

new Observable(observer =>
   console.log('this is the observer'); //doesn't gets logged
   this.alertObserver = observer
);

I have tried importing Observer through

import {Observable} from 'rxjs/Observable'; 

and

import {Observable} from 'rxjs/Rx';

but with no help.

Any suggestion ?

Upvotes: 3

Views: 2631

Answers (2)

Chuck
Chuck

Reputation: 11

Not this:

import {Observable} from 'rxjs/Rx';

This: import {Observable} from 'rxjs';

Upvotes: 1

Michael Kang
Michael Kang

Reputation: 52847

Rx observables are lazy loaded. You need at least one subscriber.

Try:

var observable = Observable.create(observer =>
   console.log('this is the observer'); 
   this.alertObserver = observer
);
observable.subscribe();

Upvotes: 7

Related Questions