Code Explainers

Code explainers tagged #async

python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
 
app = FastAPI()
 

Building a WebSocket chat with FastAPI

websockets broadcast connection-management
Intermediate 9 steps
rust
use axum::{
    extract::{Path, State},
    response::sse::{Event, KeepAlive, Sse},
};

Streaming import progress with SSE in Axum

server-sent-events streams watch-channel
Advanced 7 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react';
 
const cache = new Map();
const inflight = new Map();

Building a stale-while-revalidate hook in React

caching request-deduplication custom-hooks
Advanced 10 steps
rust
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::{interval, MissedTickBehavior};
 

A token-bucket rate limiter in Tokio

rate-limiting channels async
Advanced 8 steps
python
from fastapi import Depends, FastAPI, Header, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
 

Chaining tenant dependencies in FastAPI

dependency injection multi-tenancy authentication
Intermediate 7 steps
typescript
import { useCallback, useRef, useState } from "react";
 
type Tag = { id: string; label: string };
 

A debounced, abortable tag-input hook in React

custom-hooks debounce abort-controller
Advanced 10 steps
rust
use std::sync::Mutex;
use std::time::{Duration, Instant};
 
#[derive(Debug, Clone, Copy, PartialEq)]

Building a circuit breaker in Rust

circuit-breaker state-machine concurrency
Advanced 8 steps
javascript
class RequestDeduplicator {
  constructor(fetcher) {
    this.fetcher = fetcher;
    this.inFlight = new Map();

Deduplicating in-flight requests in JavaScript

promises caching async
Intermediate 6 steps
rust
use std::collections::HashMap;
use std::future::Future;
use std::hash::Hash;
use std::sync::{Arc, Mutex};

Deduplicating concurrent work in Rust

concurrency deduplication async
Advanced 7 steps
typescript
type RetryableRequest<T> = () => Promise<T>;
 
interface RetryOptions {
  maxAttempts?: number;

Retrying HTTP requests with backoff

retry exponential-backoff error-handling
Intermediate 8 steps
python
from fastapi import APIRouter, status
from fastapi.responses import JSONResponse
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError

Liveness vs readiness health checks in FastAPI

health-checks dependency-injection async
Intermediate 6 steps
typescript
import { useState, useEffect, useRef, useCallback } from "react";
 
interface Suggestion {
  id: string;

A debounced autocomplete hook in React

debounce custom-hooks abortcontroller
Advanced 7 steps
python
import base64
import json
from typing import Annotated, Optional
 

Cursor pagination in a FastAPI endpoint

pagination cursor async
Intermediate 9 steps
typescript
type Middleware<TIn, TOut> = (ctx: TIn) => Promise<TOut> | TOut;
 
class Pipeline<TIn, TOut> {
  private constructor(private readonly run: Middleware<TIn, TOut>) {}

A type-safe async middleware pipeline

generics type-safety middleware
Advanced 9 steps
rust
use axum::{
    extract::Query,
    response::IntoResponse,
    Json,

Parsing query strings in Axum handlers

deserialization query-parameters defaults
Intermediate 7 steps
rust
use axum::{
    body::{Body, Bytes},
    extract::Request,
    http::StatusCode,

Logging request and response sizes in Axum

middleware http streaming-bodies
Advanced 8 steps
python
import asyncio
from dataclasses import dataclass
 
import aiohttp

Bounded-concurrency HTTP fetching with asyncio

async concurrency semaphore
Intermediate 8 steps
rust
use std::{collections::HashSet, sync::Arc};
 
use axum::{
    body::Body,

Feature-flag middleware in Axum

middleware async shared-state
Advanced 7 steps