How to return an empty observable

according to this answer How to return an empty observable in rxjs , I tried all this, but it did not work for me

here is my code

post(url):Observalbe<any>{ if(this.holder.nullConnection()){ //checking internet connection //return empty observalbe return Observable.empty() ; }else{ return this.http.post(this.configurator.restServerBaseUrl+url,data) .map((result:Response)=> { return result.json() }) } } 

I tried almost all the answers according to the stack question, which you can see above any help to return empty information.

+5
source share
2 answers

Observable.of() should work for your case:

 import {Observable} from 'rxjs/Observable'; import 'rxjs/add/observable/of'; // or import {Observable} from 'rxjs/Rx'; return Observable.of(); 

or

 import {EmptyObservable} from 'rxjs/EmptyObservable'; return new EmptyObservable(); 
+10
source

In angular 2, you can return an empty observable using this:

 import {EmptyObservable} from 'rxjs/EmptyObservable'; return new EmptyObservable(); 
+1
source

Source: https://habr.com/ru/post/1260807/


All Articles