how to return an empty observable

14,059

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();
Share:
14,059

Related videos on Youtube

Mohan Gopi
Author by

Mohan Gopi

Each and every end of the day thinking what i have learned today. And Improving my self daily as developer.

Updated on June 04, 2022

Comments

  • Mohan Gopi
    Mohan Gopi almost 2 years

    according to this answer How to return an empty observable in rxjs i have tried all of it 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 have tried almost all the answers as per the stack question you can see above any help to return an empty obserable.

  • Mohan Gopi
    Mohan Gopi over 7 years
    i tried both as i am using visual studio and my editor is showing error on of() do we have to import anything like EmptyObservable
  • Mohan Gopi
    Mohan Gopi over 7 years
    as you can see my else part is working fine only i am struggling to retrun an empty observable
  • Suraj Rao
    Suraj Rao over 7 years
    u have to import the of operator
  • Mohan Gopi
    Mohan Gopi over 7 years
    it is saying that cannot find name of() like that
  • Suraj Rao
    Suraj Rao over 7 years
    import 'rxjs/add/observable/of';
  • Mohan Gopi
    Mohan Gopi over 7 years
    @GünterZöchbauer [ts] Property 'of' does not exist on type 'typeof Observable'. this is the error