Code Explainers
Code explainers tagged #async-await
javascript
async function uploadInBatches(records, uploadFn, { batchSize = 100, concurrency = 3 } = {}) { const batches = []; for (let i = 0; i < records.length; i += batchSize) { batches.push(records.slice(i, i + batchSize));
Uploading records with bounded concurrency
concurrency
worker-pool
async-await
Advanced
8 steps
typescript
export class Semaphore { private available: number; private readonly waiters: Array<() => void> = [];
Building an async Semaphore in TypeScript
concurrency
async-await
promises
Advanced
6 steps
typescript
type Task<T> = () => Promise<T>; export class Mutex { private queue: Array<() => void> = [];
Building an async mutex in TypeScript
concurrency
promises
locking
Advanced
7 steps
javascript
const express = require('express'); const router = express.Router(); const db = require('../db');
Building a paginated articles endpoint in Express
pagination
input-validation
async-await
Intermediate
8 steps
javascript
const express = require('express'); const router = express.Router(); router.get('/articles/:slug', async (req, res, next) => {
Content negotiation with res.format in Express
content-negotiation
routing
error-handling
Intermediate
9 steps
typescript
interface PollOptions<T> { intervalMs?: number; timeoutMs?: number; signal?: AbortSignal;
A cancellable polling helper in TypeScript
polling
async-await
abortsignal
Intermediate
9 steps
javascript
const asyncHandler = (fn) => (req, res, next) => { Promise.resolve(fn(req, res, next)).catch(next); };
Async error handling in Express routes
async-await
error-handling
middleware
Intermediate
7 steps
javascript
async function mapWithConcurrency(items, limit, worker) { const results = new Array(items.length); let nextIndex = 0;
Bounded-concurrency async map in JavaScript
concurrency
async-await
promises
Intermediate
7 steps
typescript
type RetryOptions = { retries?: number; timeoutMs?: number; baseDelayMs?: number;
Retry with timeout and backoff in TypeScript
promises
retry
exponential-backoff
Intermediate
10 steps
typescript
export function chunk<T>(items: readonly T[], size: number): T[][] { if (size <= 0 || !Number.isInteger(size)) { throw new RangeError(`chunk size must be a positive integer, got ${size}`); }
Splitting work into sequential batches in TypeScript
generics
async-await
batching
Intermediate
5 steps
javascript
async function fetchAllPages(baseUrl, { pageSize = 100, headers = {} } = {}) { const results = []; let cursor = null;
Cursor-based pagination with fetch
pagination
async-await
fetch
Intermediate
6 steps
javascript
async function pollJobUntilComplete(jobId, { interval = 2000, timeout = 60000, signal } = {}) { const deadline = Date.now() + timeout; while (true) {
Polling a job until it finishes in JavaScript
polling
async-await
abortsignal
Intermediate
6 steps