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

pcancellable

v0.0.1-rc.1

Published

Wrapper to create cancellable promises

Readme

Cancellable (this package in development, do not use it)

Wrapper to create cancellable promises.

A Promise cannot be canceled since once it is created and fulfillment or a rejection handler is registered to it, there is no external mechanism to stop its progression. A Cancellable wraps the ES6 standard Promise, and it is compatible with whatever promise-consuming tool.

Status

Travis Greenkeeper badge

Installation

npm install --save pcancellable

or

yarn add pcancellable

What is a Cancellable

A Cancellable implements the same methods of a standard ES6 Promise, however:

  • It can be canceled. Once the .cancel() method is called it notifies all registered resolution handlers.
  • The constructor executor parameter receives an additional onCancel argument is executed once the .cancel() is called.

API

Cancellable

The constructor has a single parameter - the Cancellable resolver, which is a function that is passed with the arguments resolve, reject and onCancel. The onCancel is a function that receives an handler which that is called once the Cancellable is canceled.

const delay = delta => {
  return new Cancellable((resolve, reject, onCancel) => {
    const id = setTimeout(() => {
      resolve(id);
    }, delta);

    // Called when canceled.
    onCancel(() => {
      clearTimeout(id);

      console.log(`Cancelled! ${id}`);
    });
  });
};

// Without cancelation.
delay(100)
  .then(console.log); // > '1'

// With cancelation.
delay(100)
  .then(console.log) // Not called.
  .cancel(); // > 'Cancelled 1'

Static methods

Cancellable.all(iterable: Iterable<T>): Cancellable<Array<T>>

Has the same behaviour as the Promise.all method, except when it is canceled it cancels all Cancellables included on the iterable argument.

Returns a cancellable that either fulfills when all of the values in the iterable argument have fulfilled or rejects as soon as one of the cancellables in the iterable argument rejects. This method wraps the Promise.all method and creates a list of cancellables that are canceled when .cancel() is called.

// Without cancelation.
Cancellable
  .all(['foo', delay(1), delay(2)])
  .then(console.log); // > ['foo', 1, 2]

// With cancelation.
Cancellable
  .all([delay(1), delay(2)])
  .then(console.log); // Not called.
  .cancel()
  // > Cancelled 1
  // > Cancelled 2

Cancellable.race(iterable: Iterable<T>): Cancellable<T>

Has the same behaviour as the Promise.race method, except when it is canceled it cancels all Cancellables included on the iterable argument. Returns a cancellable that fulfills or rejects as soon as one of the cancellables in the iterable fulfills or rejects, with the value or reason from that cancellable. This method wraps the Promise.all method and creates a list of cancellables that are canceled when .cancel() is called.

// Without cancelation.
Cancellable
  .race([delay(1), delay(2)])
  .then(console.log); // > 1

// With cancelation.
Cancellable
  .all([delay(1), delay(2)])
  .then(console.log); // Not called.
  .cancel()
  // > Cancelled 1
  // > Cancelled 2

Cancellable.resolve(value: any)

Has the same behavior as the Promise.resolve method. Returns a Cancellable object that is resolved with the given value. If the value is a thenable (i.e. has a then method), the returned cancellable will unwrap that thenable, adopting its eventual state. Otherwise the returned cancellable will be fulfilled with the value.

Cancellable.reject(value: any)

Has the same behavior as the Promise.reject method. Returns a Cancellable object that is rejected with the given reason.

Cancellable.isCancellable(value: any): boolean

Determines whether the passed value is a Cancellable.

Instance methods

Cancellable.prototype.isCanceled(): boolean

Determines whether the created Cancellable is canceled.

Cancellable.prototype.cancel(callback?: Function)

Cancels the Cancellable. It iterates upwards the chain canceling all the registered cancellables including its children. Unlike other implementations that rejects the promise when it is canceled, the cancel method receives an optional callback that is passed to the onCancel function. This way it is possible to cancel a cancellable without unhandled rejections.

const delay = delta => new Cancellable((resolve, reject, onCancel) => {
  const id = setTimeout(() => {
    resolve();
  });

  onCancel(cb => {
    clearTimeout(id);
    cb(id);
  });
});

delta(1000).cancel(() => {
  console.log(`Timeout "${id}" was canceled!`)
}); // > Timeout "1" was canceled!

Cancellable.prototype.catch(onRejected: Function): Cancellable

Has the same behavior of Promise.catch method. Appends a rejection handler callback to the cancellable, and returns a new Cancellable resolving to the return value of the callback if it is called, or to its original fulfillment value if the cancellable is instead fulfilled.

Cancellable.prototype.then(onFullfilled: Function, onRejected: Function): Cancellable

Has the same behavior of Promise.then method. Appends fulfillment and rejection handlers to the cancellable, and returns a new Cancellable resolving to the return value of the called handler, or to its original settled value if the promise was not handled.

Licence

MIT © João Granado