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 1.1.0 to 2.0.0

172

index.d.ts

@@ -1,39 +0,51 @@

/**
* 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 {
(cancelHandler: () => void): void;
shouldReject: boolean;
declare class CancelErrorClass extends Error {
readonly name: 'CancelError';
readonly isCanceled: true;
constructor(reason?: string);
}
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.
*/
interface OnCancelFunction {
(cancelHandler: () => void): void;
shouldReject: boolean;
}
type CancelError = CancelErrorClass;
}
declare class PCancelable<ValueType> extends Promise<ValueType> {
/**
* Convenience method to make your promise-returning or async function cancelable.
*
* @param fn - A promise-returning function. The function you specify will have `onCancel` appended to its parameters.
*
* @example
*
* import PCancelable from '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();
*/
Convenience method to make your promise-returning or async function cancelable.
@param fn - A promise-returning function. The function you specify will have `onCancel` appended to its parameters.
@example
```
import PCancelable = require('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();
```
*/
static fn<ReturnType>(
userFn: (onCancel: OnCancelFunction) => PromiseLike<ReturnType>
userFn: (onCancel: PCancelable.OnCancelFunction) => PromiseLike<ReturnType>
): () => PCancelable<ReturnType>;

@@ -43,3 +55,3 @@ static fn<Agument1Type, ReturnType>(

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

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

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

@@ -63,3 +75,3 @@ ): (

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

@@ -77,3 +89,3 @@ ): (

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

@@ -100,3 +112,3 @@ ): (

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

@@ -115,25 +127,26 @@ ): (

/**
* 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 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
*/
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(

@@ -143,3 +156,3 @@ executor: (

reject: (reason?: unknown) => void,
onCancel: OnCancelFunction
onCancel: PCancelable.OnCancelFunction
) => void

@@ -149,28 +162,23 @@ );

/**
* Whether the promise is canceled.
*/
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 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;
}
export default PCancelable;
/**
Rejection reason when `.cancel()` is called.
/**
* Rejection reason when `.cancel()` is called.
*
* It includes a `.isCanceled` property for convenience.
*/
export class CancelError extends Error {
readonly name: 'CancelError';
readonly isCanceled: true;
It includes a `.isCanceled` property for convenience.
*/
static CancelError: typeof CancelErrorClass;
}
constructor(reason?: string);
}
export = PCancelable;

@@ -16,6 +16,7 @@ 'use strict';

static fn(userFn) {
return (...args) => {
return (...arguments_) => {
return new PCancelable((resolve, reject, onCancel) => {
args.push(onCancel);
userFn(...args).then(resolve, reject);
arguments_.push(onCancel);
// eslint-disable-next-line promise/prefer-await-to-then
userFn(...arguments_).then(resolve, reject);
});

@@ -45,2 +46,6 @@ };

const onCancel = handler => {
if (!this._isPending) {
throw new Error('The `onCancel` handler was attached after the promise settled.');
}
this._cancelHandlers.push(handler);

@@ -52,4 +57,4 @@ };

get: () => this._rejectOnCancel,
set: bool => {
this._rejectOnCancel = bool;
set: boolean => {
this._rejectOnCancel = boolean;
}

@@ -64,2 +69,3 @@ }

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

@@ -105,4 +111,2 @@ }

module.exports = PCancelable;
module.exports.default = PCancelable;
module.exports.CancelError = CancelError;
{
"name": "p-cancelable",
"version": "1.1.0",
"version": "2.0.0",
"description": "Create a promise that can be canceled",

@@ -13,6 +13,6 @@ "license": "MIT",

"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},

@@ -44,8 +44,8 @@ "files": [

"devDependencies": {
"ava": "^1.3.1",
"ava": "^1.4.1",
"delay": "^4.1.0",
"promise.prototype.finally": "^3.1.0",
"tsd-check": "^0.3.0",
"tsd": "^0.7.1",
"xo": "^0.24.0"
}
}
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