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

@figliolia/promises

v1.0.1

Published

A small promise-wrapping library providing Cancellable and Timed Promises

Downloads

68

Readme

Promises

A small wrapper library around the native Promise API that provides two core utilities - Cancellable Promises and Timed Promises.

A Cancellable Promise is simply a Promise with a cancel method attached to it. Using it you can stop promises mid-flight and even cancel in progress network requests.

A Timed Promise is niche utility that allows a promises pending duration to be measured against a provided threshold. If the promise resolves or rejects, it'll do so yielding the result as well as the remaining milliseconds of the threshold.

Installation

npm i @figliolia/promises
# or
yarn add @figliolia/promises

Basic Usage

Cancellable Promise

import { CancellablePromise } from "@figliolia/promises";

const myAsyncWork = (signal: AbortSignal) => {
  return fetch("/some-data", { signal });
}

const CP = new CancellablePromise(myAsyncWork);
await CP.run();
// To cancel:
CP.cancel()

In the above example, we've wrapped our asynchronous task in a function accepting an AbortSignal. The CancellablePromise interface will provide your asynchronous task with a signal that you can pass down into further asynchronous work - yielding you the ability to cancel any number of asynchronous tasks at once with a call to cancel().

Timed Promise

import { TimedPromise } from "@figliolia/promises";

const myAsyncWork = () => {
  // trigger a loading spinner or something
  return fetch("/some-data", { signal });
}

const TP = new TimedPromise(myAsyncWork, 1000);
const { result, remainingMS } = await TP.run();
if(remainingMS > 0) {
  // Defer hiding the loading state so soon that
  // it may be a jarring visual to some users
  setTimeout(/* hide loading state */, remainingMS);
} else {
  // Carry about your business
}

Composing them Together

import { 
  TimedPromise,
  CancellablePromise
} from "@figliolia/promises";

const myAsyncWork = () => {
  // trigger a loading spinner or something
  return fetch("/some-data", { signal });
}

const CP = new CancellablePromise(myAsyncWork);
const TP = new TimedPromise(() => CP.run(), 1000);
const { result, remainingMS } = await TP.run();
// or
CP.cancel() // which would cancel your TP and CP at once!

Background and Contributions

I often find myself spinning up utilities such as these on fly, then kicking myself when I have to write them again in a new project or company. If you find yourself in my shoes, please feel free to PR this repository with your utilities and polyfills and I'll review promptly!