Async Validator sends request to JSON each time, when typing in Email form. It checks existing of email, for typing each letter. How can call server and check once, not on every single letter?
isEmailExist(): AsyncValidatorFn {
return (control: AbstractControl): Observable<any> => {
return this.usersService.getUserByEmail(control.value).pipe(
debounceTime(2000),
map(users => {
if (users.some(user => user.email.toLowerCase() === control.value.toLowerCase())) {
return { isExist: true };
} else {
return null;
}
})
)
}
}