QuickSnip

Tag: utility

Abort a fetch request if it takes longer than the given timeout.

export async function fetchWithTimeout(url, ms = 5000, options = {}) {
  const controller = new AbortController();
  const timer = setTimeout(() => controller.abort(), ms);
  try {
    const res = await fetch(url, { ...options, signal: controller.signal });
    return await res.json();
  } finally {
    clearTimeout(timer);

+2 more lines…

Updated Jul 17, 2026

Python one-liner to flatten a list of lists.

def flatten(nested):
    """Flatten an arbitrarily nested list of lists."""
    return [
        item
        for element in nested
        for item in (flatten(element) if isinstance(element, list) else [element])
    ]

+1 more lines…

Updated Jul 17, 2026

Idiomatic Rust file read with proper error propagation.

use std::fs;
use std::io;

fn read_config(path: &str) -> io::Result<String> {
    let contents = fs::read_to_string(path)?;
    Ok(contents)
}

+6 more lines…

Updated Jul 17, 2026

Iterate over matching files and run a command on each.

#!/usr/bin/env bash
set -euo pipefail

for file in src/**/*.ts; do
  echo "Processing $file"
  npx prettier --write "$file"
done
Updated Jul 17, 2026