Code Explainers

Code explainers tagged #async

rust
use axum::{extract::{Path, State}, http::StatusCode, Json};
use dashmap::DashMap;
use serde::Serialize;
use std::sync::Arc;

Request coalescing in an Axum handler

caching concurrency request-coalescing
Advanced 8 steps
python
from functools import wraps
import asyncio
 
from fastapi import APIRouter, FastAPI, Request

Per-route request timeouts in FastAPI

decorators async timeouts
Intermediate 6 steps
typescript
export async function copyToClipboard(text: string): Promise<boolean> {
  if (navigator.clipboard && window.isSecureContext) {
    try {
      await navigator.clipboard.writeText(text);

Clipboard copy with a legacy fallback

clipboard feature-detection graceful-degradation
Intermediate 7 steps
rust
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::IntoResponse,

Building a GitHub proxy handler in Axum

shared-state http-client error-mapping
Intermediate 9 steps
javascript
import { useEffect, useState } from 'react';
 
export function useJobStatus(jobId, { interval = 3000 } = {}) {
  const [status, setStatus] = useState('pending');

Polling a job status with a React hook

custom-hooks polling cleanup
Intermediate 8 steps
typescript
import slugify from "slugify";
 
interface SlugRepository {
  exists(slug: string): Promise<boolean>;

Generating unique URL slugs from titles

slugs normalization uniqueness
Intermediate 6 steps
rust
use axum::{
    body::Body,
    extract::State,
    http::{header, StatusCode},

Streaming NDJSON from Postgres in Axum

streaming backpressure async
Advanced 9 steps
java
public class RequestCoalescer<K, V> {
 
    private final ConcurrentHashMap<K, CompletableFuture<V>> inFlight = new ConcurrentHashMap<>();
    private final Function<K, V> loader;

Coalescing duplicate requests in Java

concurrency caching completablefuture
Advanced 6 steps
python
import logging
import uuid
from contextvars import ContextVar
 

Request ID tracing in FastAPI middleware

middleware context-variables request-tracing
Intermediate 7 steps
rust
use std::sync::Arc;
use std::time::Duration;
 
use axum::{

Long-polling with Axum and Tokio Notify

long-polling concurrency shared-state
Advanced 8 steps
typescript
type AsyncMethod = (...args: any[]) => Promise<any>;
 
function LogExecutionTime(thresholdMs = 0) {
  return function <T extends AsyncMethod>(

A method decorator that times async calls

decorators higher-order-functions async
Advanced 9 steps
javascript
class InfiniteScroll {
  constructor(sentinel, { loadMore, root = null, rootMargin = '200px' } = {}) {
    this.sentinel = sentinel;
    this.loadMore = loadMore;

Infinite scroll with IntersectionObserver

intersectionobserver pagination async
Intermediate 10 steps
typescript
import { useState, useCallback } from 'react';
 
type Todo = {
  id: string;

Optimistic UI updates in a React hook

optimistic-updates custom-hooks error-handling
Intermediate 8 steps
rust
use axum::{
    body::{Body, Bytes},
    extract::Request,
    http::StatusCode,

Logging request bodies in Axum middleware

middleware streaming-body logging
Intermediate 8 steps
rust
use axum::{
    extract::State,
    response::sse::{Event, KeepAlive, Sse},
};

Broadcasting live prices over SSE in Axum

server-sent-events broadcast-channel streams
Advanced 9 steps
javascript
import { db } from '@/lib/db'
 
const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL ?? 'https://example.com'
 

Generating a dynamic sitemap in Next.js

sitemap seo database-query
Intermediate 5 steps
php
final class DebouncedSessionStore
{
    private array $pending = [];
    private array $timers = [];

Debouncing session writes with an event loop

debounce event-loop atomic-write
Advanced 9 steps
rust
use std::collections::HashSet;
use std::path::PathBuf;
use std::time::Duration;
 

Debouncing filesystem events in async Rust

debounce async channels
Advanced 7 steps