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

promise-cancelable

v2.1.1

Published

Wrapper to create cancelable promises

Downloads

196

Readme

Cancelable

Wrapper to create cancelable 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 Cancelable wraps the ES6 standard Promise, and it is compatible with whatever promise-consuming tool.

Status

Travis Greenkeeper badge

Installation

npm install --save promise-cancelable

or

yarn add promise-cancelable

What is a Cancelable

A Cancelable 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

Cancelable

The constructor has a single parameter - the Cancelable 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 Cancelable is canceled.

const delay = delta => {
  return new Cancelable((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

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

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

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

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

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

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

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

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

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

Cancelable.resolve(value: any)

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

Cancelable.reject(value: any)

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

Cancelable.isCancelable(value: any): boolean

Determines whether the passed value is a Cancelable.

Instance methods

Cancelable.prototype.isCanceled(): boolean

Determines whether the created Cancelable is canceled.

Cancelable.prototype.cancel(callback?: Function)

Cancels the Cancelable. It iterates upwards the chain canceling all the registered cancelables 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 cancelable without unhandled rejections.

const delay = delta => new Cancelable((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!

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

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

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

Has the same behavior of Promise.then method. Appends fulfillment and rejection handlers to the cancelable, and returns a new Cancelable 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