npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@azghr/singlet

v0.1.9

Published

Deduplicate concurrent async calls: same key, one in-flight promise. Not a cache.

Readme

singlet

npm MIT License

Deduplicate concurrent async calls. Same key → one in-flight promise. Not a cache.

The problem

The same async operation often fires many times at once:

  • Three components mount and each fetch /users/42 → 3 HTTP requests
  • A webhook burst triggers five identical DB refreshes → 5 database queries
  • Two clicks race the same mutation → conflicting writes

You get N network calls, N database hits, and sometimes N conflicting writes — for one answer.

The solution

singlet solves concurrent call deduplication — not caching:

import singlet from "@azghr/singlet";

const flight = singlet();

// These 3 concurrent calls → 1 HTTP request:
const [user1, user2, user3] = await Promise.all([
  flight.run(`user:42`, () => fetch('/api/users/42').then(r => r.json())),
  flight.run(`user:42`, () => fetch('/api/users/42').then(r => r.json())),
  flight.run(`user:42`, () => fetch('/api/users/42').then(r => r.json()))
]);

// After settlement → key is forgotten (not a cache):
const later = await flight.run(`user:42`, () => fetch('/api/users/42').then(r => r.json()));
// This makes a fresh request

Install

npm install @azghr/singlet
# or
pnpm add @azghr/singlet
# or
yarn add @azghr/singlet

Use

Core API

import singlet from "@azghr/singlet";

const flight = singlet();

async function getUser(id: string) {
  return flight.run(`user:${id}`, () =>
    fetch(`/api/users/${id}`).then(r => r.json())
  );
}

// Concurrent calls → ONE fetch
const [user1, user2, user3] = await Promise.all([
  getUser("42"),
  getUser("42"),
  getUser("42")
]);

Ergonomic API

import { wrap } from "@azghr/singlet";

const fetchUser = wrap(
  async (id: string) => fetch(`/api/users/${id}`).then(r => r.json()),
  { keyFn: ([id]) => `user:${id}` }
);

// These concurrent calls share ONE fetch:
const [user1, user2] = await Promise.all([
  fetchUser("42"),
  fetchUser("42")
]);

API

Core functions

singlet(): Singlet

Creates an isolated singlet instance with its own key namespace.

Singlet.run<T>(key: string, fn: () => T | Promise<T>): Promise<T>

If key is in flight, returns the existing promise without calling fn. Otherwise calls fn, shares its promise with concurrent callers, and forgets the key once it settles.

Singlet.forget(key: string): boolean

Drops an in-flight key so the next run starts fresh.

Singlet.getInFlightKeys(): string[]

Returns a snapshot of all keys currently in flight. Useful for monitoring and debugging.

Singlet.isInFlight(key: string): boolean · Singlet.size: number

Introspection for tests, metrics, and debugging.

Ergonomic functions

wrap<TArgs, TResult>(fn: (...args: TArgs) => TResult | Promise<TResult>, options: WrapOptions<TArgs>): (...args: TArgs) => Promise<TResult>

Wraps a function to automatically deduplicate concurrent calls based on its arguments.

wrapWithKey<TResult>(fn: () => TResult | Promise<TResult>, key: string, singlet?: Singlet): () => Promise<TResult>

Wraps a no-argument function with a fixed deduplication key.

Shared instance

shared: Singlet

A shared app-wide instance for simple use cases.

import { shared } from "@azghr/singlet";
await shared.run("config", loadConfig);

Fixed-key deduplication: wrapWithKey()

For single operations that shouldn't run concurrently:

import { wrapWithKey } from "@azghr/singlet";

const loadConfig = wrapWithKey(
  async () => fetch('/api/config').then(r => r.json()),
  'app:config'
);

// Multiple components loading config concurrently → ONE fetch:
const [config1, config2] = await Promise.all([
  loadConfig(),
  loadConfig()
]);

Patterns & composition

Deduplication + caching

Singlet prevents concurrent duplicate work; cache libraries store results. Compose them for complete deduplication:

import { wrap } from "@azghr/singlet";

const cache = new Map<string, User>();

const getUser = wrap(
  async (id: string) => {
    const hit = cache.get(id);
    if (hit) return hit;

    const user = await fetchUser(id);
    cache.set(id, user);
    return user;
  },
  { keyFn: ([id]) => `user:${id}` }
);

Framework integration

Singlet is framework-agnostic. Integrate with any framework:

// React hook example
import { wrap } from "@azghr/singlet";

const useUser = (id: string) => {
  const fetchUser = wrap(
    async (userId: string) => fetch(`/api/users/${userId}`).then(r => r.json()),
    { keyFn: ([userId]) => `user:${userId}` }
  );

  // Use in React, Vue, Svelte, etc.
  const { data, loading } = useFetch(() => fetchUser(id));
  return { user: data, loading };
};

Non-goals

By design, singlet focuses on one thing: deduplicating concurrent in-flight calls. These features are explicitly out of scope:

  • Result caching/TTLs — Use cache libraries (lru-cache, node-cache)
  • Retries/backoff — Use retry libraries (p-retry, retry)
  • Timeouts — Use timeout libraries (p-timeout, Promise.race)
  • Batching/DataLoader — Use batching libraries (dataloader, batch-promises)
  • Framework adapters — Compose with React/Vue/Angular hooks

If you need these features, compose singlet with specialized libraries.

Related Packages

Caching & Concurrency:

  • @azghr/filterkit — Framework-agnostic, type-safe filtering for TypeScript
  • staleness — Stale-while-revalidate caching for async functions

Text Processing:

  • @azghr/shorn — Truncate strings by byte budget without breaking graphemes
  • seriatim — Sequential processing utilities

HTTP & Network:

  • forbear — Read server rate-limit instructions from HTTP responses
  • forestall — Delay execution until a condition is met
  • obviate — Render operations unnecessary through caching

System & Process:

  • quiesce — Ordered, timeboxed graceful shutdown for Node
  • sortition — Deterministic percentage rollouts and A/B bucketing
  • stanch — Stop flows or operations based on conditions

Utilities:

  • expunge — Remove or exclude items from collections
  • occlude — Hide or mask data and functionality
  • placemark — Geographic location and mapping utilities
  • specie — Currency and financial calculations

License

MIT