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

coalesced

v1.0.1

Published

Lightweight async task coalescer. Prevents overlapping execution and coalesces multiple calls into at most one rerun.

Readme

Coalesced Runner

Lightweight async task coalescing with deterministic execution. Prevents race conditions by ensuring tasks run sequentially and coalesce repeated calls.


Features

  • No race conditions — never runs the same task concurrently
  • Coalescing — multiple calls collapse into a single rerun
  • Event-driven — lifecycle events (start, success, error, drain)
  • Fail-fast by default — errors crash if unhandled
  • Key-based isolation — separate execution per key
  • Memory-safe — no function retention, automatic cleanup

Installation

npm install coalesced-runner

Quick Start

import { CoalescedRunner } from "coalesced-runner";

const runner = new CoalescedRunner();

const task = async () => {
  console.log("run");
};

runner.run(task);
runner.run(task);
runner.run(task);

Output

run
run

Multiple calls are coalesced into:

  • 1 execution immediately
  • 1 rerun after completion (if triggered while running)

How It Works

run() ──▶ [RUNNING]
            │
            ├─ run() → mark pending
            ├─ run() → still pending (no queue)
            │
            ▼
         [DONE]
            │
            └─ pending? → run once more

Why this exists

In async systems, this pattern is common:

await updateState();
await updateState();
await updateState();

Without control, this causes:

  • race conditions
  • duplicated work
  • inconsistent state

CoalescedRunner guarantees:

  • only one task runs at a time
  • repeated calls are merged into at most one rerun

Key-based Execution

runner.runWithKey("user:1", async () => {
  // runs independently from other keys
});

Each key has its own isolated queue.


Events

The runner is event-driven using a familiar EventEmitter API.

Available Events

| Event | Description | | --------- | --------------------------- | | start | Task execution started | | success | Task completed successfully | | error | Task failed | | drain | No more pending tasks (per fn/key) |


Example

import { RunnerEvents } from "coalesced-runner";

runner.on(RunnerEvents.ERROR, (err) => {
  console.error(err.meta, err.cause);
});

runner.on(RunnerEvents.DRAIN, (meta) => {
  console.log("idle");
});

Meta (Typed Context)

Each event receives a meta object describing the task.

type RunnerMeta =
  | { type: "fn"; name?: string }
  | { type: "key"; key: string | symbol; name?: string };

Example

runner.on(RunnerEvents.START, (meta) => {
  if (meta.type === "key") {
    console.log(meta.key);
  }
});

Error Handling Behavior

  • If an error listener exists → error is emitted
  • If no listener → error is thrown asynchronously (fail-fast)

API


runner.run(fn)

Run using function identity (===).

const fn = async () => {};

runner.run(fn);
runner.run(fn); // coalesced

runner.runWithKey(key, fn)

Run using explicit key.

runner.runWithKey("user:1", async () => {
  await syncUser(1);
});

Key type

type RunnerKey = string | symbol;

runner.clearKey(key)

Manually clear internal state.

runner.clearKey("user:1");

Important Notes

1. This is NOT a queue

run()
run()
run()

Not: run 3 times Actually: run 2 times


2. Function identity matters

runner.run(async () => {});
runner.run(async () => {});

NOT coalesced (different functions)


3. Use keys for dynamic tasks

runner.runWithKey(`user:${id}`, fn);

4. Does NOT fix race inside your function

const fn = async () => {
  // your logic must still be safe
};

Runner only controls execution timing, not data correctness.


5. Fire-and-forget

runner.run(fn);
  • does NOT return a Promise
  • errors handled via event

Example: Prevent Spam

const save = async () => {
  await api.save();
};

button.onclick = () => {
  runner.run(save);
};

spam click → no overload


Example: Sync Latest State

runner.runWithKey("sync", async () => {
  await syncState();
});

ensures:

  • no overlap
  • always runs latest update

When to Use

Use this when you need:

  • prevent overlapping async tasks
  • avoid queue buildup
  • ensure latest update is applied
  • handle burst events safely

When NOT to Use

  • need strict ordering → use queue
  • need concurrency → use semaphore
  • need dedupe only → use single-flight

Comparison

| Pattern | Behavior | | -------------------- | ---------------- | | Queue | runs all tasks | | Debounce | delay + collapse | | Throttle | limit rate | | Single-flight | run once only | | Coalesced Runner | run + 1 rerun |


License

MIT