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 0.5.0 to 0.5.1

40

index.js

@@ -28,2 +28,3 @@ 'use strict';

this._isCanceled = false;
this._rejectOnCancel = true;

@@ -33,15 +34,26 @@ this._promise = new Promise((resolve, reject) => {

return executor(
value => {
this._isPending = false;
resolve(value);
},
error => {
this._isPending = false;
reject(error);
},
handler => {
this._cancelHandlers.push(handler);
const onResolve = value => {
this._isPending = false;
resolve(value);
};
const onReject = error => {
this._isPending = false;
reject(error);
};
const onCancel = handler => {
this._cancelHandlers.push(handler);
};
Object.defineProperties(onCancel, {
shouldReject: {
get: () => this._rejectOnCancel,
set: bool => {
this._rejectOnCancel = bool;
}
}
);
});
return executor(onResolve, onReject, onCancel);
});

@@ -78,3 +90,5 @@ }

this._isCanceled = true;
this._reject(new CancelError(reason));
if (this._rejectOnCancel) {
this._reject(new CancelError(reason));
}
}

@@ -81,0 +95,0 @@

{
"name": "p-cancelable",
"version": "0.5.0",
"version": "0.5.1",
"description": "Create a promise that can be canceled",

@@ -45,4 +45,4 @@ "license": "MIT",

"promise.prototype.finally": "^3.1.0",
"xo": "*"
"xo": "^0.23.0"
}
}

@@ -56,4 +56,20 @@ # p-cancelable [![Build Status](https://travis-ci.org/sindresorhus/p-cancelable.svg?branch=master)](https://travis-ci.org/sindresorhus/p-cancelable)

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`.
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`.
```js
const cancelable = new PCancelable((resolve, reject, onCancel) => {
const job = new Job();
onCancel.shouldReject = false;
onCancel(() => {
job.stop();
});
job.on('finish', resolve);
});
promise.cancel(); // Doesn't throw an error
```
`PCancelable` is a subclass of `Promise`.

@@ -60,0 +76,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