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 🙏

© 2024 – Pkg Stats / Ryan Hefner

abortify

v2.0.4

Published

Create abortable async functions with ease

Downloads

3

Readme

abortify

Create abortable async functions with ease

Abortable

An abortable is an asynchronous function that can be aborted. An abortable is passed a resolve and reject callback, just like the Promise constructor, and returns a function for aborting the operation. Consider the following definition of an abortable version of setTimeout:

import type { Abortable } from "abortify";

export function setAbortableTimeout(ms: number): Abortable<void> {
  return (resolve, reject) => {
    const handle = setTimeout(resolve, ms);
    return () => clearTimeout(handle);
  };
}

abortify

The abortify function converts abortable functions into functions that return a Promise. It works just like util.promisify, with a few additions:

  • The function will accept an AbortSignal as an optional final argument.
  • The function will throw an AbortError when the signal is aborted.
  • The abortable will be aborted before the promise resolves or rejects.

Consider the following definition of an abortified version of the setAbortableTimeout above:

import { abortify } from "abortify";

export const sleep = abortify(setAbortableTimeout);

// The sleep function above has the following signature:
// (ms: number, signal?: AbortSignal) => Promise<void>

Using abortified functions

Once you have abortified a function you use it just like any other async function. Consider the following example:

import { sleep } from "abortify";

// this function will print a message every second
// this function will only exit when the signal is aborted
async function forever(signal?: AbortSignal) {
  for (;;) {
    await sleep(1000, signal);
    console.log("another second has passed");
  }
}

async function main() {
  const controller = new AbortController();

  // abort after a short amount of time
  setTimeout(() => controller.abort(), 5000);

  try {
    await forever(controller.signal);
  } catch (err) {
    if (err.aborted) {
      console.error(err); // AbortError: The operation was aborted
    }
  }
}

main();