ngrx - Angular 2 emit when Http has finished to subsequently run another function -
i have general function doing delete call api. function this:
deleteitem(url, item, action) { return this.http.delete(url) .subscribe( () => this.store.dispatch({ type: action, payload: item }), error => this.errorhandler(error), () => console.debug('delete complete') ) ;
i call function few places sending in different urls, items , actions. let's i've got function this:
deletebookcase(bookcase) { this.apiservice.deleteitem(bookcase_url, bookcase, bookcase_remove); }
sometimes, i'd trigger action once item has been deleted api. example, maybe want check whether global store of books has changed removed bookcase.
is there simple way let deletebookcase
functions know http call , subsequent actions have completed before prematurely triggering action?
use map()
instead of subscribe()
in deleteitem()
deleteitem(url, item, action) { return this.http.delete(url) .map(() => this.store.dispatch({ type: action, payload: item }) .catch((error) => this.errorhandler(error)) .do(() => console.debug('delete complete')) }); }
and subscribe call deleteitem()
deletebookcase(bookcase) { this.apiservice.deleteitem(bookcase_url, bookcase, bookcase_remove) .subscribe(data => dosomething()); }
don't forget import do
, map
, catch
Comments
Post a Comment