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

@oraichain/p-cancelable

v4.0.2

Published

Create a promise that can be canceled

Downloads

9

Readme

p-cancelable

Create a promise that can be canceled

Useful for animation, loading resources, long-running async computations, async iteration, etc.

If you target Node.js 16 or later, this package is less useful and you should probably use AbortController instead.

Install

npm install p-cancelable

Usage

import PCancelable from "@oraichain/p-cancelable";

const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
	const worker = new SomeLongRunningOperation();

	onCancel(() => {
		worker.close();
	});

	worker.on("finish", resolve);
	worker.on("error", reject);
});

// Cancel the operation after 10 seconds
setTimeout(() => {
	cancelablePromise.cancel("Unicorn has changed its color");
}, 10000);

try {
	console.log("Operation finished successfully:", await cancelablePromise);
} catch (error) {
	if (cancelablePromise.isCanceled) {
		// Handle the cancelation here
		console.log("Operation was canceled");
		return;
	}

	throw error;
}

API

new PCancelable(executor)

Same as the Promise constructor, but with an appended onCancel parameter in executor.

Cancelling will reject the promise with CancelError. To avoid that, set onCancel.shouldReject to false.

import PCancelable from "@oraichain/p-cancelable";

const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
	const job = new Job();

	onCancel.shouldReject = false;
	onCancel(() => {
		job.stop();
	});

	job.on("finish", resolve);
});

cancelablePromise.cancel(); // Doesn't throw an error

PCancelable is a subclass of Promise.

onCanceled(fn)

Type: Function

Accepts a function that is called when the promise is canceled.

You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.

PCancelable#cancel(reason?)

Type: Function

Cancel the promise and optionally provide a reason.

The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.

PCancelable#isCanceled

Type: boolean

Whether the promise is canceled.

PCancelable.fn(fn)

Convenience method to make your promise-returning or async function cancelable.

The function you specify will have onCancel appended to its parameters.

import PCancelable from "@oraichain/p-cancelable";

const fn = PCancelable.fn((input, onCancel) => {
	const job = new Job();

	onCancel(() => {
		job.cleanup();
	});

	return job.start(); //=> Promise
});

const cancelablePromise = fn("input"); //=> PCancelable

// …

cancelablePromise.cancel();

CancelError

Type: Error

Rejection reason when .cancel() is called.

It includes a .isCanceled property for convenience.

FAQ

Cancelable vs. Cancellable

In American English, the verb cancel is usually inflected canceled and canceling—with one l. Both a browser API and the Cancelable Promises proposal use this spelling.

What about the official Cancelable Promises proposal?

~~It's still an early draft and I don't really like its current direction. It complicates everything and will require deep changes in the ecosystem to adapt to it. And the way you have to use cancel tokens is verbose and convoluted. I much prefer the more pragmatic and less invasive approach in this module.~~ The proposal was withdrawn.

p-cancelable for enterprise

Available as part of the Tidelift Subscription.

The maintainers of p-cancelable and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Related

  • p-progress - Create a promise that reports progress
  • p-lazy - Create a lazy promise that defers execution until .then() or .catch() is called
  • p-signal - Cancel promises using AbortSignal
  • More…