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

async-scope-js

v0.1.1

Published

Structured concurrency for JavaScript with scoped async tasks and cancellation.

Readme

async-scope

Structured concurrency for JavaScript & TypeScript async-scope brings Go-style errgroup semantics and structured concurrency to JavaScript.

It solves a fundamental problem in async JS:

Promises make failures contagious. Structured concurrency makes failures controlled.

The Problem

In JavaScript today:

Promise.all([fetchUser(), fetchBilling(), fetchPermissions()]);

If any promise fails:

  • Everything rejects
  • Partial work is lost
  • Cancellation is manual
  • Errors leak across layers

This creates:

  • Cascading failures
  • Resource leaks
  • Race conditions
  • Complex error handling

JavaScript has no built-in concept of a scope for async work.

The Solution: Async Scopes

async-scope introduces structured concurrency:

  • All async work runs inside a scope
  • ancellation propagates downward
  • Errors are collected, not leaked
  • Only the scope decides success or failure
  • This is how Go, Rust, Kotlin, and Swift handle concurrency.

Now JavaScript can too.

Basic Usage

import { withScope } from "async-scope";

await withScope(async (scope) => {
    scope.spawn(async () => {
        await fetchUser();
    });

    scope.spawn(async () => {
        await fetchBilling();
    });

    scope.spawn(async () => {
        await fetchPermissions();
    });
});

If any task fails:

  • All siblings are cancelled
  • The scope fails
  • No leaks, no partial state

Policies

Scopes support two failure policies:

Policy Behavior

  • cancel (default) First failure cancels all tasks
  • supervise Tasks may fail without cancelling siblings
await withScope(async (scope) => {
    scope.spawn(async () => {
        throw new Error("non-fatal");
    });

    scope.spawn(async () => {
        await doImportantWork();
    });
}, { policy: "supervise" });

Optional Tasks

For work that should never affect the scope:

const metrics = await scope.spawnOptional(async () => {
    // metrics is undefined if it fails
    return await fetchMetrics();
});

Optional tasks:

  • Never cancel
  • Never fail the scope
  • Are still tracked and cancelled when needed

Critical Tasks

For work that must always stop everything on failure:

scope.spawnCritical(async () => {
    await commitTransaction();
});

Critical tasks:

  • Cancel the scope immediately
  • Override supervise mode

Nested Scopes

Scopes can be nested safely.

scope.spawn(async () => {
    await withScope(async (child) => {
        child.spawn(async () => {
            await doChildWork();
        });
    });
});

Rules:

  • Parent cancellation → cancels child
  • Child failure → does not cancel parent
  • Errors surface only when awaited

Cancellation

scope.cancel();

Cancels:

  • All tasks
  • All nested scopes
  • All future spawns

Tasks receive an AbortSignal:

scope.spawn(async (signal) => {
    while (!signal.aborted) {
        await doWork();
    }
});

Business Rules

Scenario | Result spawn() | fails, policy = cancel Cancel all siblings spawn() | fails, policy = supervise Continue spawnCritical() | fails Always cancel spawnOptional() | fails Ignored Child scope fails | Parent unaffected Parent cancelled | Child cancelled User function throws | Scope fails waitForCompletion() | called Errors are thrown

Error Handling

All scope-related failures use ScopeError.

import { ScopeError } from "async-scope";

try {
    await withScope(async (scope) => {
        scope.spawn(async () => {
            throw new Error("boom");
        });
    });
} catch (err) {
    if (err instanceof ScopeError) {
        // structured cancellation or task failure
        console.log(err.reason);
    } else {
        throw err;
    }
}

Inspecting Partial Failures

In supervise mode you can inspect what failed:

await withScope(async (scope) => {
    scope.spawn(async () => {
        throw new Error("A");
    });

    scope.spawn(async () => {
        throw new Error("B");
    });

    scope.spawn(async () => {
        await importantWork();
    });

    await scope.waitForCompletion();

    console.log(scope.getErrors()); // [Error("A"), Error("B")]
}, { policy: "supervise" });

Why this is safer than Promises

Promises async-scope Errors leak Errors contained No cancellation Structured cancellation Partial state All-or-nothing Race conditions Deterministic Hard to reason Hierarchical

Why This Matters

This is the same model used by:

  • Go (errgroup)
  • Kotlin (coroutineScope)
  • Swift (TaskGroup)
  • Rust (tokio::scope)

Now JavaScript has it too.

Summary

async-scope gives you:

  • Structured concurrency
  • Predictable cancellation
  • Contained failures
  • Safe nesting
  • Production-grade async control

Stop fighting promises.

Start using scopes.