Community Snippets

Public code snippets shared by developers. Search, copy, or share your own.

Share a snippet

Async HTTP Fetch with httpx

Python
import asyncio
import httpx

async func fetch_url(url: str) -> dict:
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        return response.json()

# Utilisation : data = asyncio.run(fetch_url("https://api.example.com/data"))
#python#asyncio#httpx
by Abba Sali Aboubakar Mamate

Go HTTP JSON Server

Go
package main

import (
	"encoding/json"
	"net/http"
)

func main() {
	http.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
	})
	http.ListenAndServe(":8080", nil)
}
#go#http#api
by Abba Sali Aboubakar Mamate

Debounce Function

JavaScript
function debounce(fn, delay = 300) {
  let timeoutId;
  return (...args) => {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => fn(...args), delay);
  };
}
#performance#utils#events
by Abba Sali Aboubakar Mamate

Fetch JSON avec Try/Catch

JavaScript
async function fetchData(url) {
  try {
    const response = await fetch(url);
    if (!response.ok) throw new Error(`Erreur HTTP: ${response.status}`);
    return await response.json();
  } catch (error) {
    console.error("Échec de la requête :", error.message);
  }
}
#fetch#api#async
by Abba Sali Aboubakar Mamate

Debounce a function

TypeScript

A minimal debounce helper — click into it to see the full snippet page.

function debounce<T extends (...args: unknown[]) => void>(fn: T, delayMs: number) {
  let timer: ReturnType<typeof setTimeout>;
  return (...args: Parameters<T>) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delayMs);
  };
}
#demo#typescript#utility
by DevTools Cloud