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 🙏

© 2025 – Pkg Stats / Ryan Hefner

asyncsignal

v0.0.5

Published

Reusable asynchronous signals,it's a like promisewithResolvers

Readme

AsyncSignal

Reusable asynchronous signals for JavaScript/TypeScript applications.

中文

Installation

pnpm add asyncsignal
// or
npm install asyncsignal
// or
yarn add asyncsignal

Features

  • 🚦 Reusable Signals: Create async signals that can be reused after reset
  • 🔒 Constraint Support: Add conditions that must be met before signal resolution
  • ⏱️ Timeout Control: Set timeout for signal resolution with customizable behavior
  • 🎯 Signal States: Track signal states (pending, resolved, rejected)
  • 🎮 Signal Management: Manage multiple signals with AsyncSignalManager
  • 🔄 Reset Capability: Reset signals to their initial state for reuse
  • 💪 TypeScript Support: Full TypeScript support with type definitions

Usage

Basic Usage

import { asyncSignal } from "asyncsignal";

// Create a basic signal
const signal = asyncSignal();

// Wait for signal resolution
await signal();

// Resolve the signal
signal.resolve("success");

// Reject the signal
signal.reject(new Error("something went wrong"));

// Reset the signal for reuse
signal.reset();

Advanced Features

Constraint Functions

// Signal will only resolve when the constraint function returns true
const signal = asyncSignal(() => someCondition === true);

// Attempting to resolve when constraint is not met will be ignored
signal.resolve(); // Will only resolve if someCondition === true

Timeout Control

// Create signal with default timeout
const signal = asyncSignal(undefined, { timeout: 1000 });

// Wait with timeout and default value
await signal(2000); // Will resolve after 2 seconds

// Wait with timeout and error
await signal(2000, new Error("Timeout occurred")); // Will reject with error after 2 seconds

Signal Events

import { asyncSignal } from "asyncsignal";
const signal = asyncSignal();
// when signal resolve/reject, after reset willtrigger
signal.on((e?: Error, result?: any) => {});
// when signal resolve/reject, after reset will not trigger
signal.once((e?: Error, result?: any) => {});

State Checking

const signal = asyncSignal();

console.log(signal.isPending()); // true after creation
console.log(signal.isResolved()); // true after resolution
console.log(signal.isRejected()); // true after rejection

Error Handling

import { asyncSignal, AsyncSignalAbort } from "asyncsignal";

// Handle signal destruction
const signal = asyncSignal();
try {
  await signal();
} catch (error) {
  if (error instanceof AsyncSignalAbort) {
    console.log("Signal was destroyed");
  }
}

Implementation Details

  • Signal states are mutually exclusive (pending, resolved, rejected)
  • Calling resolve() or reject() on a non-pending signal will be ignored
  • Calling resolve() when the constraint function returns false will be silently ignored
  • The destroy() method will reject pending signals with AsyncSignalAbort error

Managing Multiple Signals

import { AsyncSignalManager } from "asyncsignal";

// Create a signal manager with default timeout
const manager = new AsyncSignalManager({ timeout: 5000 });

// Create multiple signals
const signal1 = manager.create();
const signal2 = manager.create(() => someCondition);

// Resolve all signals with optional value
manager.resolve("success");

// Reject all signals
manager.reject(new Error("batch operation failed"));

// Reset all signals for reuse
manager.reset();

// Destroy specific signals
manager.destroy(signal1.id);

// Destroy multiple signals
manager.destroy([signal1.id, signal2.id]);

// Destroy all signals
manager.destroy();

Open Source Projects