import { Injectable } from '@angular/core'; import { FormControl } from '@angular/forms'; import { RegistrationService } from '../registration.service'; @Injectable() export class UsernameValidator { debouncer: any; constructor( private registrationService: RegistrationService ){ } checkUsername( control: FormControl ): any{ clearTimeout(this.debouncer); return new Promise(resolve => { this.debouncer = setTimeout(() => { this.registrationService.validateUserName(control.value).subscribe((res) => { console.log(res.users.length) if(res.users.length === 0){ resolve(null); }else{ resolve({'usernameInUse': true}); } /* if(res.ok){ resolve(null); } }, (err) => { resolve({'usernameInUse': true}); */ }); }, 1000); }); } }