1

I need to Final Count to always be 1, this is storing the fail request count. I am also unsure as why the Final Count is printed before the Initial Count. I am not understanding the subscribe properly maybe. Hopefully this makes sense.

 public failed: number = 0;

    buttonClickMethod(): void {
        // request code

        Observable.onErrorResumeNext(myrequests).first().subscribe(
            () => {
                // On next code
            },
            () => {
                this.failed++;
                console.log('Initial count' + this.failed)
            },
            () => {
                //On completed code
            }
        });
    console.log('Final count' + this.failed)
    this.failed = 0;

    }
3
  • Could you include more code of your class? Commented Sep 24, 2018 at 18:55
  • Final count is called before initial count, because the myrequests does some async actions (I expect it calls a backend service). The function in the subscribe block is only called after the requests completes. But this only is for the callbacks in the subscribe method. The code after subscribe is executed immediatly. Commented Sep 26, 2018 at 11:23
  • Maybe you could fix your problem by moving the console.log statement into the on complete callback. Anyway, it is not very clear what you want to do. Why should the final count be always 1? Are you assuming that the request always fails? Otherwise I would expect the final count to be 0 if the request succeeded. Commented Sep 26, 2018 at 11:24

1 Answer 1

1
public failed: number = 0;

buttonClickMethod(): void {
    // request code
    Observable.onErrorResumeNext(myrequests).first().subscribe(
        () => {
            // On next code
        },
        () => {
            this.failed++;
            console.log('Initial count' + this.failed)
        },
        () => {
            //On completed code
        }
    });
console.log('Final count' + this.failed)
this.failed = 0;

}

Your console.log statement is on button click which will get executed immediately, unlike your Observable which is async in nature.

Have a look at how async calls work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.