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

abortable-promise-chain

v1.0.12

Published

Promise subclass to make the promise chain abortable.

Downloads

24

Readme

abortable-promise-chain

A Promise subclass to make the promise chain abortable.

Why?

Sometimes in a promise chain, you need to abort the chain and prevent any additional then/catch/finally from running.

In simple cases, this can be done by just throwing an exception and catching it at the end of the chain. But let's imagine a more complex chain: multiple different catch handlers, handling different kinds of exceptions, only some of which are fatal. As complexity grows it's harder and harder (also uglier and harder to read) to make this happen.

That's why I wrote this package.

Installation

  1. To install the package, just run:
npm install abortable-promise-chain --save
  1. Import it in your code and start using it:
import AbortablePromise from 'abortable-promise-chain';

AbortablePromise.from(someApiCall())
  .then((res, abort) => {
    if (res.isTheSkyBlue) {
      abort();
    }
    return res;
  })
  .then(res => {
    // this won't run.
  });

If you are using it in node, require it like this:

const AbortablePromise = require('abortable-promise-chain').default;

Usage

There are two ways of creating an AbortablePromise:

  1. From an executor function, just like we would use for creating a normal Promise:
const executor = (resolve, reject) => setTimeout(() => resolve('data'), 1000);
const abortable = new AbortablePromise(executor);
  1. From an already created normal Promise. (Note that we are using the from static method instead of the constructor here)
const promise = fetch('some url');
const abortable = AbortablePromise.from(promise);

Our AbortablePromise is working the same as a normal Promise, except for one thing.

Every handler we pass to the then / catch / finally methods is getting an extra, second parameter which is the abort function. This can be used to (surprise!) abort the chain. That means no additional then / catch / finally handlers will be called and the promise will never be resolved or rejected.

Note that finally normally doesn't get any parameters, but we still use the second parameter for the abort function (for consistency), so you need to ignore the first one (which is always undefined).

// ...continuing the previous example

abortable
  .then((res, abort) => {
    // i can call abort here,
  })
  .catch((err, abort) => {
    // or here,
  })
  .finally((_, abort) => {
    // or here.
  });

Unlike some alternatives, AbortablePromise allows you to abort the chain from the inside. And even if you return a normal Promise or a value from a handler, it gets automatically wrapped, so you don't loose the abort capability further down.

AbortablePromise.from(fetch('json url'))
  .then(res => res.json()) // that's a normal promise,
  .then((res, abort) => {
    // but i can still call abort here,
    if (res.isTheSeaWet) {
      abort();
    }
    return res;
  })
  .then(res => {
    // so this won't run.
  });

Supported browsers

The package is transpiled to ES5 with Babel, using the "defaults" browserlist. You can run npx browserslist "defaults" in your terminal to see the list of browsers.

Node.js is also supported.

Pros

There are some other packages for a similar use case, but this one is:

  • Tiny: 764 bytes of minified JavaScript.
  • Well tested: 100% test coverage.
  • Safe: No known vulnerabilities according to npm audit.
  • Self contained: No external dependencies (only devDependencies).

Contributing

If you would like to contribute or report an issue, please read Contributing and our Code of Conduct.

Done

That's it, I hope you find this useful.