简体   繁体   中英

Complete callback in Observable.prototype.subscribe in Angular 2

The complete callback does not work as expected. Let me explain:

See this picture, note the complete callback in subscribe method. This complete function is only called when the observerOrNext is called. When some error happens, the complete is not called. This is right? There are another method to get a callback that always is called when the process finish?

在此处输入图片说明

Example:

When success:

this.getData(params)
    .subscribe(
        successData => {
            // this is called
        },
        error => {
            // this is not called. Ok!
        },
        () => { // when complete
            // this is called, ok!
        }
    );

When error:

this.getData(params)
    .subscribe(
        successData => {
            // this is not called, ok!
        },
        error => {
            // this is called. Ok! Yeah!
        },
        () => { // when complete
            // this is not called, why god??
        }
    );

I think what you are looking for is the .finally function.

Invokes a specified action after the source observable sequence terminates gracefully or exceptionally. There is an alias called finallyAction for browsers < IE9

Here's an example: finally.md .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM