how to return an empty observable

2019-02-22 02:40发布

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.

2条回答
对你真心纯属浪费
2楼-- · 2019-02-22 03:37

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();
查看更多
ら.Afraid
3楼-- · 2019-02-22 03:44

In angular 2 you can return empty observable by using this :

import {EmptyObservable} from 'rxjs/EmptyObservable';
    return new EmptyObservable();
查看更多
登录 后发表回答