Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJS pipe Finalize operator not getting called

import {
    Observable,
    BehaviorSubject
} from 'rxjs';
import {
    finalize,
    share
} from 'rxjs/operators'

export class someComponent() {

    public count$ = new BehaviorSubject < any > (0);

    public constructor() {
        this.shareResponse()
            .pipe(
                finalize(() => {
                    console.log('finalize called');
                }))
            .subscribe((event: any) => {
                // Do something
            });
    }
    public shareResponse(): Observable < any > {
        return this.count$.pipe(share());
    }
    public countChanged(event) {
        this.count$.next(event);
    }
}

HTML:

    <some-tag(countChanged) = (countChanged($event)) > < /some-tag>
like image 406
su1212 Avatar asked Dec 07 '25 08:12

su1212


1 Answers

BehaviorSubject doesn't complete unless you complete it yourself by calling this.count$.complete(). That's why finalize() is not happening, since it is waiting for Observable completion.

Have a look at the code example on StackBlitz, see link.

like image 196
Goga Koreli Avatar answered Dec 09 '25 01:12

Goga Koreli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!