Search code examples
angularrxjsangular9subscriber

pass argument from subscribe block or error block to 'finally' block


Is there a way to pass argument from subscribe block or error block to 'finally' block without using this keyword. My attempt below which does not work

this.service.create()
    .pipe(first())
    .subscribe (
    (resp) => {
        let argumentToFinally = '';
        if (!!resp.taskInfo) {
            argumentToFinally = this.showError(resp.taskInfo);
        } else {
            close();
            // if else block, 'argumentToFinally' is still ''
        }
    },
    (error) => {
        this.isCallingApi = false;
        argumentToFinally = this.showError('create', undefined, error);
    },
    (argumentToFinally) => {
        close(argumentToFinally)
    });

close(argumentOfFinally?: any) {
    if (!!argumentOfFinally) {
        ..
    }
    this.closeWizard();
}

Solution

    • You can only use the global variable (this.variableName) in the finally block as the finally block doesn't accept any argument.
    • The finally block will not be called after the error block because the subscription is canceled when an error occurs.

    For more info check -> Using observables to pass values