Skip to content

Instantly share code, notes, and snippets.

@jorroll
Created November 4, 2019 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorroll/3c25a1521abed3d7535945bbcf121c5b to your computer and use it in GitHub Desktop.
Save jorroll/3c25a1521abed3d7535945bbcf121c5b to your computer and use it in GitHub Desktop.
const usernameControl = new FormControl('');
usernameControl.validationEvents
.pipe(
filter(event => event.label === 'End'),
tap(() => this.control.markPending(true, { source: 'usernameValidator' })),
switchMap(username =>
interval(500).pipe(
take(1),
switchMap(() => this.userService.findUserByHandle(username)),
map(user => !!user),
),
),
tap(() => this.control.markPending(false, { source: 'usernameValidator' })),
)
.subscribe(usernameExists => {
const errors = usernameExists ? { usernameExists: true } : null;
this.usernameControl.setErrors(errors, {
source: 'usernameValidator',
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment