switchMap does not repeat HTTP calls.
I have created a directive that validates an email if it already exists.
Inside that directive, is an API. Every time a keypress happens, the API is being called to check if it already exists.
I'm using switchMap to prevent multiple HTTP requests and cancel previous ones.
But the problem is once the request has already been made and canceled, it would not repeat again.
e.g. test@gmail.com - canceled request
test2@gmail.com - successful API
test@gmail.com (again) - the API call will not happen anymore.
Why is this happening (to me) ??
I have checked switchMap operator docs and I do not see any situations regarding about this.
<input [(ngModel)]="sampleEmail" placeholder="Email" isEmailExistsValidator>
export class isEmailExistsValidator implements AsyncValidator {
constructor(private someService: SomeService) { }
public validate(control: AbstractControl): Observable<ValidationErrors|null> {
if (isNullOrUndefined(control.value)) {
return of(null);
}
return this.someService
.isEmailExistsValidator(control.value)
.pipe(
switchMap((response) => {
if (response.availability) {
return of(null);
} else {
return of({ emailAlreadyExists: response.response });
}
})
);
}
}
public isEmailExistsValidator(email: string): Observable<EmailAvailability> {
return this.httpClient.get<EmailAvailable>('/api/sampleEmailValidation/' + email);
}
I expect the API to be successful in any email input.