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

cancellable-chain-of-promises

v0.1.3

Published

Library to write cancellable chain of Promises, using the This-Binding Syntax proposal.

Downloads

3

Readme

Cancellable Chain Of Promises

npm version Build Status

A library to write cancellable chain of promises.

This library is inspired by the This-Binding Syntax proposal. If you don't want to depend on this proposal, you can have a look at the like-bind-operator library.

Examples

import CancelToken, { Cancelled } from 'cancellable-chain-of-promises';

const token = new CancelToken((cancel) => {
    cancelButton.onclick = cancel;
});

// Write your asynchronous code:
const promise = token.resolve()
  ::token.then(doSomething)
  ::token.then(doSomethingElse)
  ::token.catch(handleError);

// If you cancel, the "promise" object will reject with an Cancelled error.

Using like-bind-operator

import CancelToken, { Cancelled } from 'cancellable-chain-of-promises';
import $ from 'like-bind-operator';

const token = new CancelToken((cancel) => {
    cancelButton.onclick = cancel;
});

// Write your asynchronous code:
const promise = token.resolve()
  [$](token.then)(doSomething)
  [$](token.then)(doSomethingElse)
  [$](token.catch)(handleError);

// If you cancel, the "promise" object will reject with an Cancelled error.

Documentation

This library is still experimental and may change in future releases.

CancelToken

The CancelToken object is used to represent a cancellable operation.

Constructor: new CancelToken([callback] [, ...parentTokens])

  • callback: A function that get the cancel function as a parameter.
  • parentTokens: Tokens that will propagate their cancelled state to this token.

token.chain:

An object providing several utility functions to chain promises in a cancellable way.

token.chain.then: (alias: token.then) promise::token.chain.then(onFulfilled[, onRejected])

Similar to Promise.prototype.then. If the token is in a cancelled state, onFulfilled and onRejected will not be called, and the returned promise will reject with the Cancelled error.

token.chain.catch: (alias: token.catch) promise::token.chain.catch(onRejected)

Similar to Promise.prototype.catch. If the token is in a cancelled state, onRejected will not be called, and the returned promise will reject with the Cancelled error.

token.newPromise: token.newPromise((resolve, reject) => {})

A Promise factory, that returns a rejected Promise if the token is cancelled, or construct a new Promise. The callback is not called is the token is cancelled.

token.resolve: token.resolve(value)

A function that returns a rejected Promise if the token is cancelled, or a Promise resolved with the given value.

token.reject: token.reject(value)

A function that returns a rejected Promise if the token is cancelled, or a Promise rejected with the given value.

Cancelled

An Cancelled is used to represent the cancellation of an operation. It is the rejected value to propagate the cancellation through a chain of promises.

Utility functions

always: (aliases: token.chain.always, token.always) promise::always(callback)

Use always to always call a callback in a chain of promises. The returned or thrown value

Other Examples

Cancellable Request

const request = (url, { method, body, cancelToken }) => {
  const token = new CancelToken(cancelToken);
  let cancelListener;
  return new Promise(function (resolve, reject) {
    const xhr = new XMLHttpRequest();
    cancelListener = (cancelError) => {
      xhr.abort();
      reject(cancelError);
    };
    token.addCancelListener(cancelListener);
    xhr.open(method, url);
    xhr.onload = function () {
      if (this.status >= 200 && this.status < 300) {
        resolve(xhr.response);
      } else {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      }
    };
    xhr.onerror = function () {
      reject({
        status: this.status,
        statusText: xhr.statusText
      });
    };
    xhr.send(body);
  })::always(() => token.removeCancelListener(cancelListener))
};

Cancel Previous Operations

let previousCancel = null;
function example() {
  let clean = null;
  if (previousCancel) {
    previousCancel();
  }

  const token = new CancelToken((cancel) => {
    previousCancel = cancel;
    clean = () => {
      if (previousCancel === cancel) {
        previousCancel = null;
      }
    };
  });

  request('/example', {cancelToken: this.token})
    ::this.token.chain.then(response => processResponse(response))
    ::always(clean);
}

Cancel Operations When Removing a Widget

class Widget {
  constructor() {
    this.token = new CancelToken(cancel => {
      this.cancel = cancel;
    });
  }

  destroy() {
    this.cancel();
  }

  onclick() {
    request('/random', {cancelToken: this.token})
      ::this.token.chain.then(response => this.updateState(response));
  }

  // other methods ...
}

Cancellable setTimeout

const setCancellableTimeout = (fn, duration, token) => {
  if (!token.aborted) {
    let id = 0;
    const cancel = () => {
      cancelTimeout(id);
      token.removeCancelListener(cancel);
    };
    id = setTimeout(() => {
      token.removeCancelListener(cancel);
      fn();
    }, duration);
  }
};