Search code examples
angularrxjsobservableabortrxjs5

how to abort and "resolve" a subscription immediately in rxjs?


New to rxjs and angular2. How do I abort an http call and return an observable so I don't get an error on the subscription end? of course if I just return an object here, we get .searchAlbum(...).subscribe is not a function

public searchAlbum (term:string, queryObject?:any) {

    if( this.abortSearches){
      queryObject.body = null;
      return //what to return here? used to be deferred.resolve(queryObject);
    }

    ...

    return this.http.request( new Request( options ) )
        .map(res => this.extractData(res, queryObject) )
        .catch(this.handleError);
}

Solution

  • As far as I understand your situation you want to return an Observable even though the this.abortSearches is true and therefore you don't want to make any actual HTTP request. However, you have some other functionality tied to this method so you want it to always return an Observable.

    You can return Observable.empty() which is an Observable that emits no values an just signals complete.