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.3.0 to 0.4.0

55

index.js

@@ -8,11 +8,15 @@ 'use strict';

}
get isCanceled() {
return true;
}
}
class PCancelable {
static fn(fn) {
static fn(userFn) {
return function () {
const args = [].slice.apply(arguments);
return new PCancelable((onCancel, resolve, reject) => {
args.unshift(onCancel);
fn.apply(null, args).then(resolve, reject);
return new PCancelable((resolve, reject, onCancel) => {
args.push(onCancel);
userFn.apply(null, args).then(resolve, reject);
});

@@ -23,4 +27,5 @@ };

constructor(executor) {
this._pending = true;
this._canceled = false;
this._cancelHandlers = [];
this._isPending = true;
this._isCanceled = false;

@@ -31,12 +36,12 @@ this._promise = new Promise((resolve, reject) => {

return executor(
fn => {
this._cancel = fn;
value => {
this._isPending = false;
resolve(value);
},
val => {
this._pending = false;
resolve(val);
error => {
this._isPending = false;
reject(error);
},
err => {
this._pending = false;
reject(err);
handler => {
this._cancelHandlers.push(handler);
}

@@ -47,18 +52,20 @@ );

then() {
return this._promise.then.apply(this._promise, arguments);
then(onFulfilled, onRejected) {
return this._promise.then(onFulfilled, onRejected);
}
catch() {
return this._promise.catch.apply(this._promise, arguments);
catch(onRejected) {
return this._promise.catch(onRejected);
}
cancel() {
if (!this._pending || this._canceled) {
if (!this._isPending || this._isCanceled) {
return;
}
if (typeof this._cancel === 'function') {
if (this._cancelHandlers.length > 0) {
try {
this._cancel();
for (const handler of this._cancelHandlers) {
handler();
}
} catch (err) {

@@ -69,8 +76,8 @@ this._reject(err);

this._canceled = true;
this._isCanceled = true;
this._reject(new CancelError());
}
get canceled() {
return this._canceled;
get isCanceled() {
return this._isCanceled;
}

@@ -77,0 +84,0 @@ }

{
"name": "p-cancelable",
"version": "0.3.0",
"description": "Create a promise that can be canceled",
"license": "MIT",
"repository": "sindresorhus/p-cancelable",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"promise",
"cancelable",
"cancel",
"canceled",
"canceling",
"cancellable",
"cancellation",
"abort",
"abortable",
"aborting",
"cleanup",
"task",
"token",
"async",
"function",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"xo": "*"
}
"name": "p-cancelable",
"version": "0.4.0",
"description": "Create a promise that can be canceled",
"license": "MIT",
"repository": "sindresorhus/p-cancelable",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"promise",
"cancelable",
"cancel",
"canceled",
"canceling",
"cancellable",
"cancellation",
"abort",
"abortable",
"aborting",
"cleanup",
"task",
"token",
"async",
"function",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"xo": "*"
}
}

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

```
$ npm install --save p-cancelable
$ npm install p-cancelable
```

@@ -21,3 +21,3 @@

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

@@ -37,4 +37,4 @@

})
.catch(reason => {
if (cancelablePromise.canceled) {
.catch(error => {
if (cancelablePromise.isCanceled) {
// Handle the cancelation here

@@ -45,3 +45,3 @@ console.log('Operation was canceled');

throw reason;
throw error;
});

@@ -60,3 +60,3 @@

Same as the [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with a prepended `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`.

@@ -71,3 +71,3 @@ `PCancelable` is a subclass of `Promise`.

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

@@ -78,7 +78,7 @@ ### PCancelable#cancel()

Cancel the promise. The cancellation is synchronous.
Cancel the promise.
Calling it after the promise has settled or multiple times does nothing.
The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
### PCancelable#canceled
### PCancelable#isCanceled

@@ -95,2 +95,4 @@ Type: `boolean`

It includes a `.isCanceled` property for convenience.
### PCancelable.fn(fn)

@@ -100,6 +102,6 @@

The function you specify will have `onCancel` prepended to its parameters.
The function you specify will have `onCancel` appended to its parameters.
```js
const fn = PCancelable.fn((onCancel, input) => {
const fn = PCancelable.fn((input, onCancel) => {
const job = new Job();

@@ -135,2 +137,3 @@

- [p-progress](https://github.com/sindresorhus/p-progress) - Create a promise that reports progress
- [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called

@@ -137,0 +140,0 @@ - [More…](https://github.com/sindresorhus/promise-fun)

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