Socket
Socket
Sign inDemoInstall

p-cancelable

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.1 to 3.0.0

140

index.d.ts

@@ -1,2 +0,7 @@

declare class CancelErrorClass extends Error {
/**
The rejection reason when `.cancel()` is called.
It includes a `.isCanceled` property for convenience.
*/
export class CancelError extends Error {
readonly name: 'CancelError';

@@ -8,17 +13,60 @@ readonly isCanceled: true;

declare namespace PCancelable {
/**
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.
*/
export interface OnCancelFunction {
shouldReject: boolean;
(cancelHandler: () => void): void;
}
export default class PCancelable<ValueType> extends Promise<ValueType> {
/**
Accepts a function that is called when the promise is canceled.
Whether the promise is canceled.
*/
readonly isCanceled: boolean;
You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.
/**
Cancel the promise and optionally provide a reason.
The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
@param reason - The cancellation reason to reject the promise with.
*/
interface OnCancelFunction {
(cancelHandler: () => void): void;
shouldReject: boolean;
}
cancel: (reason?: string) => void;
type CancelError = CancelErrorClass;
}
/**
Create a promise that can be canceled.
declare class PCancelable<ValueType> extends Promise<ValueType> {
Can be constructed in the same was as a [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`. `PCancelable` is a subclass of `Promise`.
Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
@example
```
import PCancelable from '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
```
*/
constructor(
executor: (
resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
reject: (reason?: unknown) => void,
onCancel: OnCancelFunction
) => void
);
/**

@@ -31,3 +79,3 @@ Convenience method to make your promise-returning or async function cancelable.

```
import PCancelable = require('p-cancelable');
import PCancelable from 'p-cancelable';

@@ -52,3 +100,3 @@ const fn = PCancelable.fn((input, onCancel) => {

static fn<ReturnType>(
userFn: (onCancel: PCancelable.OnCancelFunction) => PromiseLike<ReturnType>
userFn: (onCancel: OnCancelFunction) => PromiseLike<ReturnType>
): () => PCancelable<ReturnType>;

@@ -58,3 +106,3 @@ static fn<Agument1Type, ReturnType>(

argument1: Agument1Type,
onCancel: PCancelable.OnCancelFunction
onCancel: OnCancelFunction
) => PromiseLike<ReturnType>

@@ -66,3 +114,3 @@ ): (argument1: Agument1Type) => PCancelable<ReturnType>;

argument2: Agument2Type,
onCancel: PCancelable.OnCancelFunction
onCancel: OnCancelFunction
) => PromiseLike<ReturnType>

@@ -78,3 +126,3 @@ ): (

argument3: Agument3Type,
onCancel: PCancelable.OnCancelFunction
onCancel: OnCancelFunction
) => PromiseLike<ReturnType>

@@ -92,3 +140,3 @@ ): (

argument4: Agument4Type,
onCancel: PCancelable.OnCancelFunction
onCancel: OnCancelFunction
) => PromiseLike<ReturnType>

@@ -115,3 +163,3 @@ ): (

argument5: Agument5Type,
onCancel: PCancelable.OnCancelFunction
onCancel: OnCancelFunction
) => PromiseLike<ReturnType>

@@ -128,58 +176,2 @@ ): (

): (...arguments: unknown[]) => PCancelable<ReturnType>;
/**
Create a promise that can be canceled.
Can be constructed in the same was as a [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`. `PCancelable` is a subclass of `Promise`.
Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
@example
```
import PCancelable = require('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
```
*/
constructor(
executor: (
resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
reject: (reason?: unknown) => void,
onCancel: PCancelable.OnCancelFunction
) => void
);
/**
Whether the promise is canceled.
*/
readonly isCanceled: boolean;
/**
Cancel the promise and optionally provide a reason.
The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
@param reason - The cancellation reason to reject the promise with.
*/
cancel: (reason?: string) => void;
/**
Rejection reason when `.cancel()` is called.
It includes a `.isCanceled` property for convenience.
*/
static CancelError: typeof CancelErrorClass;
}
export = PCancelable;

@@ -1,4 +0,2 @@

'use strict';
class CancelError extends Error {
export class CancelError extends Error {
constructor(reason) {

@@ -14,4 +12,6 @@ super(reason || 'Promise was canceled');

class PCancelable {
static fn(userFn) {
// TODO: Use private class fields when ESLint 8 is out.
export default class PCancelable {
static fn(userFunction) {
return (...arguments_) => {

@@ -21,3 +21,3 @@ return new PCancelable((resolve, reject, onCancel) => {

// eslint-disable-next-line promise/prefer-await-to-then
userFn(...arguments_).then(resolve, reject);
userFunction(...arguments_).then(resolve, reject);
});

@@ -65,3 +65,3 @@ };

return executor(onResolve, onReject, onCancel);
executor(onResolve, onReject, onCancel);
});

@@ -76,2 +76,3 @@ }

catch(onRejected) {
// eslint-disable-next-line promise/prefer-await-to-then
return this._promise.catch(onRejected);

@@ -81,2 +82,3 @@ }

finally(onFinally) {
// eslint-disable-next-line promise/prefer-await-to-then
return this._promise.finally(onFinally);

@@ -114,4 +116,1 @@ }

Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);
module.exports = PCancelable;
module.exports.CancelError = CancelError;
{
"name": "p-cancelable",
"version": "2.1.1",
"version": "3.0.0",
"description": "Create a promise that can be canceled",

@@ -10,6 +10,8 @@ "license": "MIT",

"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12.20"
},

@@ -44,8 +46,7 @@ "scripts": {

"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"promise.prototype.finally": "^3.1.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
"ava": "^3.15.0",
"delay": "^5.0.0",
"tsd": "^0.16.0",
"xo": "^0.40.1"
}
}

@@ -18,3 +18,3 @@ # p-cancelable

```js
const PCancelable = require('p-cancelable');
import PCancelable from 'p-cancelable';

@@ -32,16 +32,2 @@ const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {

(async () => {
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;
}
})();
// Cancel the operation after 10 seconds

@@ -51,2 +37,14 @@ setTimeout(() => {

}, 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;
}
```

@@ -58,7 +56,8 @@

Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`.<br>
Cancelling will reject the promise with `PCancelable.CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`.
Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
```js
const PCancelable = require('p-cancelable');
import PCancelable from 'p-cancelable';

@@ -103,10 +102,2 @@ const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {

### PCancelable.CancelError
Type: `Error`
Rejection reason when `.cancel()` is called.
It includes a `.isCanceled` property for convenience.
### PCancelable.fn(fn)

@@ -119,3 +110,3 @@

```js
const PCancelable = require('p-cancelable');
import PCancelable from 'p-cancelable';

@@ -139,2 +130,10 @@ const fn = PCancelable.fn((input, onCancel) => {

### CancelError
Type: `Error`
Rejection reason when `.cancel()` is called.
It includes a `.isCanceled` property for convenience.
## FAQ

@@ -144,3 +143,3 @@

[In American English, the verb cancel is usually inflected canceled and canceling—with one l.](http://grammarist.com/spelling/cancel/)<br>Both a [browser API](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable) and the [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises) use this spelling.
[In American English, the verb cancel is usually inflected canceled and canceling—with one l.](http://grammarist.com/spelling/cancel/) Both a [browser API](https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelable) and the [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises) use this spelling.

@@ -147,0 +146,0 @@ ### What about the official [Cancelable Promises proposal](https://github.com/tc39/proposal-cancelable-promises)?

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc