3

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);
}
martin
  • 93,354
  • 25
  • 191
  • 226
FlavorScape
  • 13,301
  • 12
  • 75
  • 117
  • 1
    The context for this code is not clear. It possibly should be `return Observable.of(queryObject)`. As for request cancellation, see this http://stackoverflow.com/questions/36490926/how-to-cancel-a-httprequest-in-angular-2 – Estus Flask Dec 05 '16 at 03:59

1 Answers1

2

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.

martin
  • 93,354
  • 25
  • 191
  • 226