javascript - .do() or .subscribe() -
i understand best practice of using .subscribe , .do methods of observables.
for example if need job after initial data loaded server
const init$: observable<mydata> = this._dataservice.getdata(); init$ .do((initialdata: mydata) => { this.data = initialdata; // @ step initializing view }) .switchmap(() => loadextradata) .subscribe((extradata) => { dosomething(extradata, this._data); // need this._data here }); i can same .subscribe
const init$: observable<mydata> = this._dataservice.getdata() .sharereplay(1); init$ .subscribe((initialdata: mydata) => { this.data = initialdata; // @ step initializing view }) init$ .combinelatest(loadextradata) .subscribe(([initialdata, extradata]) => { dosomething(extradata, initialdata); // need this._data here }); which 1 better , why?
the observable chain not going until use subscribe() makes connections between operators. have use subscribe() method.
the do() operator intended make side-effects.
in experience it's not possible in subscribe() , need use do() because application logic needs make actions in specific order.
so in general i'd try avoid using do() , put of logic subscribe(). if had perform actions in different order i'd use do().
Comments
Post a Comment