typescript
46 lines · 6 steps
Building an async Semaphore in TypeScript
A counting semaphore that limits how many async tasks run at once by parking excess callers as pending promises.
Explained by
highlit
1export class Semaphore {
2 private available: number;
3 private readonly waiters: Array<() => void> = [];
4
5 constructor(permits: number) {
6 if (permits < 1) throw new RangeError("permits must be >= 1");
7 this.available = permits;
8 }
9
10 async acquire(): Promise<void> {
11 if (this.available > 0) {
12 this.available--;
13 return;
14 }
15 await new Promise<void>((resolve) => this.waiters.push(resolve));
16 }
17
18 release(): void {
19 const next = this.waiters.shift();
20 if (next) {
21 next();
22 } else {
23 this.available++;
24 }
25 }
26
27 async run<T>(task: () => Promise<T>): Promise<T> {
28 await this.acquire();
29 try {
30 return await task();
31 } finally {
32 this.release();
33 }
34 }
35}
36
37export async function mapWithLimit<T, R>(
38 items: readonly T[],
39 limit: number,
40 fn: (item: T, index: number) => Promise<R>,
41): Promise<R[]> {
42 const semaphore = new Semaphore(limit);
43 return Promise.all(
44 items.map((item, index) => semaphore.run(() => fn(item, index))),
45 );
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A resolve function captured from a Promise can be stored and called later to unblock an awaiting caller.
- 2Counting permits plus a FIFO waiter queue is enough to bound concurrency without threads or locks.
- 3Wrapping work in acquire/finally-release guarantees a permit is always returned, even when the task throws.
Related explainers
java
public class SlidingLogRateLimiter { private final int maxRequests; private final long windowMillis;
How a sliding-log rate limiter works
rate-limiting
concurrency
sliding-window
Advanced
8 steps
typescript
import { Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { ConfigService } from '@nestjs/config';
How a JWT strategy authenticates in NestJS
authentication
jwt
passport
Intermediate
7 steps
go
package theme import ( "fmt"
Per-tenant HTML templates with Gin's renderer
multi-tenancy
concurrency
html-templates
Advanced
8 steps
python
import time import threading from enum import Enum from functools import wraps
Building a circuit breaker in Python
circuit-breaker
resilience
decorators
Advanced
7 steps
typescript
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpErrorResponse } from '@angular/common/http'; import { BehaviorSubject, Observable, throwError } from 'rxjs'; import { catchError, filter, take, switchMap } from 'rxjs/operators';
Refreshing auth tokens in an Angular interceptor
http-interceptor
token-refresh
rxjs
Advanced
8 steps
php
<?php namespace App\Http\Middleware;
Idempotent requests with Laravel middleware
idempotency
middleware
caching
Advanced
8 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/building-an-async-semaphore-in-typescript-explained-typescript-7dc6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.