rxjs - Subscribe twice to Angular 2 Http? -
i have service calling api processes request , performs standard error handling on api requests. instance have apierrorsservice
want call when api call gets error. want submit calls in standard way, i.e. setting content-type application/json , serializing request object.
of course when subscribe observable returned http
called once, , when return , caller subscribes called again. best way able listen results in 2 places without performing api call twice? can call .publish()
or .share()
, return that? need dispose of it? there timing issues may occur? if mock api call , return value work fine, or caller's subscription miss values? here's code have:
post(path: string, data: any): observable<response> { var headers: headers = new headers(); headers.set("content-type", "application/json"); var s = data == null ? null : json.stringify(data, null, 2); var shared = this.http.post(path, s, { headers: headers }).share(); shared.subscribe(null, err => this.apperrorsservice.addapierror(err)); return shared; }
using share
overkill. if want process error inline, can use do
instead:
return this.http.post(path, s, { headers: headers }) .do(null, err => this.apperrorsservice.addapierror(err));
Comments
Post a Comment