Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the differnce between 'do' and 'finally' when using RxJS observables to take action after the observable returns?

I don't see very good documentation on what the difference is between do and finally in RxJS. My goal is to take action only when an Observable returns data but it looks like they will both take action on failure as well.

  • do says "Invokes an action for each element in the observable sequence and invokes an action upon graceful or exceptional termination of the observable sequence."

The observable might return more than one element?

  • finally says "Invoke a specified action after the source observable sequence terminates gracefully or exceptionally".

My hope is that someone will explain if it matters which is used or if there is a better alternate method.

    getData(choice): void {
        this.dataService.getTableData(choice, 'mainCalls.php')
            .do( () => this.defineWidth() )
            .subscribe(tableData => this.tableData = tableData,
                err => {
                    console.log(err);
                }
            );
    }

    ngOnInit() {
        this.getData('getTableData');
    }

defineWidth is a function that is dependent upon the data being returned by the Observable. I'm open to suggestion and reading material on alternate methods to accomplish what I want.

like image 537
Tyler Christian Avatar asked Jan 24 '17 20:01

Tyler Christian


People also ask

What type of RxJS object is returned when subscribing to an Observable?

This call also returns an object, the Subscription : const subscription = observable.subscribe((x) => console.log(x));

What is the difference between Observable and a subject in RxJS?

An RxJS Subject is a special type of Observable that allows values to be multicasted to many Observers. While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers.

What is difference between Observable and observer in Angular?

An Observer is a consumer of values delivered by an Observable. Observers are simply a set of callbacks, one for each type of notification delivered by the Observable: next , error , and complete .

What does complete do in Observable?

Complete value: With the complete value, the observer sends no value. This usually signals that the subscriptions for that particular Observable is complete. If the complete value is sent, nothing else can be delivered to the Observable.


1 Answers

do() is called for each normal event and does not modify the data stream. It is used only for side effects.

finally() is called once after the last event or after an error if any. It is called once in either case success or failure.

If this.defineWidth() depends on this.tableData than you don't need do or finally. Just add the call after the line where you assign the response to this.tableData:

getData(choice): void {
    this.dataService.getTableData(choice, 'mainCalls.php')
        .subscribe(tableData => {
           this.tableData = tableData;
           this.defineWidth();
         }),
         err => {
           console.log(err);
         }
    );
}
like image 83
Günter Zöchbauer Avatar answered Oct 13 '22 10:10

Günter Zöchbauer