Unsubscribing to the reCAPTCHA v3 execution subscription in ng-recaptcha

44 Views Asked by At

I am using Google reCAPTCHA in my Angular 14 application. I am using the ng-recaptcha npm package, version 10. The documentation says that its important that I close the execute subscription. The subscription is started each time I click the submit button where captcha is used. The following is the save function that is using the captcha:

save() {
    let sub = this.recaptchaV3Service.execute('save').subscribe((token: string) => {
        this.doSave(token);
    });
}

I guess that each time I click the save button a new subscription is started, so placing the unsubscribe in the ngDestroy wont be a good idea. Can I place the unsubscribe directly inside the callback, like the following code?

let sub = this.recaptchaV3Service.execute('save').subscribe((token: string) => {
    this.doSave(token);
    sub.unsubscribe();
});

Is this a good way of doing it or is there a better way?

0

There are 0 best solutions below