typescript
35 lines · 6 steps
How an async username validator works in Angular
A debounced, HTTP-backed reactive form validator that checks whether a username is already taken.
Explained by
highlit
1import { Injectable } from '@angular/core';
2import { HttpClient } from '@angular/common/http';
3import {
4 AsyncValidator,
5 AbstractControl,
6 ValidationErrors,
7} from '@angular/forms';
8import { Observable, of, timer } from 'rxjs';
9import { catchError, map, switchMap } from 'rxjs/operators';
10
11@Injectable({ providedIn: 'root' })
12export class UsernameAvailabilityValidator implements AsyncValidator {
13 constructor(private http: HttpClient) {}
14
15 validate(
16 control: AbstractControl,
17 ): Observable<ValidationErrors | null> {
18 const username = (control.value ?? '').trim();
19 if (!username) {
20 return of(null);
21 }
22
23 return timer(400).pipe(
24 switchMap(() =>
25 this.http.get<{ available: boolean }>('/api/users/availability', {
26 params: { username },
27 }),
28 ),
29 map(({ available }) =>
30 available ? null : { usernameTaken: { value: username } },
31 ),
32 catchError(() => of({ availabilityCheckFailed: true })),
33 );
34 }
35}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Async validators return an Observable so form validity can depend on a server round-trip.
- 2A leading timer plus switchMap debounces input and cancels stale in-flight requests.
- 3catchError keeps a failed network call from silently blocking form submission.
Related explainers
typescript
interface ParsedName { first: string; middle: string; last: string;
Parsing human names into structured parts
parsing
string-manipulation
normalization
Intermediate
9 steps
go
package middleware import ( "compress/gzip"
How gzip HTTP middleware works in Go
middleware
compression
object-pooling
Intermediate
7 steps
typescript
import { useCallback, useEffect, useRef, useState } from "react"; interface UseResendCooldownOptions { cooldownSeconds?: number;
A resend cooldown hook in React
custom-hooks
timers
state-management
Intermediate
7 steps
typescript
import { Injectable, PipeTransform, ArgumentMetadata,
A custom validation pipe in NestJS
validation
dto
recursion
Intermediate
10 steps
rust
use std::collections::HashMap; #[derive(Debug)] pub struct RequestHead {
Parsing an HTTP request head in Rust
parsing
error-handling
iterators
Intermediate
9 steps
typescript
import { CallHandler, ExecutionContext, Injectable,
Recording HTTP metrics with a NestJS interceptor
interceptor
observability
prometheus
Intermediate
5 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-an-async-username-validator-works-in-angular-explained-typescript-d2b4/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.