cancelable-promise
Advanced tools
Comparing version 2.6.0 to 3.0.0
@@ -1,111 +0,89 @@ | ||
"use strict"; | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(mod.exports); | ||
global.CancelablePromise = mod.exports; | ||
} | ||
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
Object.defineProperty(_exports, "__esModule", { | ||
value: true | ||
}); | ||
_exports.cancelable = cancelable; | ||
_exports.CancelablePromise = CancelablePromise; | ||
_exports.default = void 0; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function createCallback(onResult, options) { | ||
if (onResult) { | ||
return function (arg) { | ||
if (!options.isCanceled) { | ||
return onResult(arg); | ||
} | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
return arg; | ||
}; | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function thenFunc(options, onSuccess, onError) { | ||
return cancelable(this.then(createCallback(onSuccess, options), createCallback(onError, options)), options); | ||
} | ||
var handleCallback = function handleCallback(resolve, reject, callback, r) { | ||
try { | ||
resolve(callback(r)); | ||
} catch (e) { | ||
reject(e); | ||
function catchFunc(options, onError) { | ||
return cancelable(this.catch(createCallback(onError, options)), options); | ||
} | ||
}; | ||
var CancelablePromise = /*#__PURE__*/function () { | ||
_createClass(CancelablePromise, null, [{ | ||
key: "all", | ||
value: function all(iterable) { | ||
return new CancelablePromise(function (y, n) { | ||
Promise.all(iterable).then(y, n); | ||
}); | ||
} | ||
}, { | ||
key: "race", | ||
value: function race(iterable) { | ||
return new CancelablePromise(function (y, n) { | ||
Promise.race(iterable).then(y, n); | ||
}); | ||
} | ||
}, { | ||
key: "reject", | ||
value: function reject(value) { | ||
return new CancelablePromise(function (y, n) { | ||
Promise.reject(value).then(y, n); | ||
}); | ||
} | ||
}, { | ||
key: "resolve", | ||
value: function resolve(value) { | ||
return new CancelablePromise(function (y, n) { | ||
Promise.resolve(value).then(y, n); | ||
}); | ||
} | ||
}]); | ||
function finallyFunc(options, onFinally) { | ||
return cancelable(this.finally(onFinally), options); | ||
} | ||
function CancelablePromise(executor) { | ||
_classCallCheck(this, CancelablePromise); | ||
function cancelFunc(options) { | ||
options.isCanceled = true; | ||
} | ||
this._promise = new Promise(executor); | ||
this._canceled = false; | ||
function cancelable(promise) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { | ||
isCanceled: false | ||
}; | ||
return { | ||
then: thenFunc.bind(promise, options), | ||
catch: catchFunc.bind(promise, options), | ||
finally: finallyFunc.bind(promise, options), | ||
cancel: cancelFunc.bind(promise, options) | ||
}; | ||
} | ||
_createClass(CancelablePromise, [{ | ||
key: "then", | ||
value: function then(success, error) { | ||
var _this = this; | ||
function CancelablePromise(executor) { | ||
return cancelable(new Promise(executor)); | ||
} | ||
var p = new CancelablePromise(function (resolve, reject) { | ||
_this._promise.then(function (r) { | ||
if (_this._canceled) { | ||
p.cancel(); | ||
} | ||
CancelablePromise.all = function (iterable) { | ||
return cancelable(Promise.all(iterable)); | ||
}; | ||
if (success && !_this._canceled) { | ||
handleCallback(resolve, reject, success, r); | ||
} else { | ||
resolve(r); | ||
} | ||
}, function (r) { | ||
if (_this._canceled) { | ||
p.cancel(); | ||
} | ||
CancelablePromise.allSettled = function (iterable) { | ||
return cancelable(Promise.allSettled(iterable)); | ||
}; | ||
if (error && !_this._canceled) { | ||
handleCallback(resolve, reject, error, r); | ||
} else { | ||
reject(r); | ||
} | ||
}); | ||
}); | ||
return p; | ||
} | ||
}, { | ||
key: "catch", | ||
value: function _catch(error) { | ||
return this.then(undefined, error); | ||
} | ||
}, { | ||
key: "cancel", | ||
value: function cancel(errorCallback) { | ||
this._canceled = true; | ||
CancelablePromise.race = function (iterable) { | ||
return cancelable(Promise.race(iterable)); | ||
}; | ||
if (errorCallback) { | ||
this._promise.catch(errorCallback); | ||
} | ||
CancelablePromise.resolve = function (value) { | ||
return cancelable(Promise.resolve(value)); | ||
}; | ||
return this; | ||
} | ||
}]); | ||
CancelablePromise.reject = function (value) { | ||
return cancelable(Promise.reject(value)); | ||
}; | ||
return CancelablePromise; | ||
}(); | ||
exports.default = CancelablePromise; | ||
var _default = CancelablePromise; | ||
_exports.default = _default; | ||
}); |
{ | ||
"name": "cancelable-promise", | ||
"version": "2.6.0", | ||
"version": "3.0.0", | ||
"description": "A simple cancelable promise", | ||
"main": "dist/CancelablePromise.js", | ||
"jsnext:main": "CancelablePromise.js", | ||
"jsnext:main": "src/CancelablePromise.js", | ||
"files": [ | ||
"dist/CancelablePromise.js", | ||
"CancelablePromise.js", | ||
"dist/CancelablePromise.min.js", | ||
"dist/CancelablePromise.d.ts", | ||
"src/CancelablePromise.js", | ||
"src/CancelablePromise.d.ts", | ||
"LICENSE", | ||
"README.md" | ||
"README.md", | ||
"CODE_OF_CONDUCT.md", | ||
"CHANGELOG.md" | ||
], | ||
"scripts": { | ||
"test": "eslint . && mocha --opts mocha.opts", | ||
"build": "babel -d dist/ CancelablePromise.js", | ||
"babel": "babel src/CancelablePromise.js -d dist", | ||
"build": "npm run clean && npm run babel && npm run uglify && npm run copy", | ||
"clean": "node scripts.js clean", | ||
"copy": "node scripts.js copy", | ||
"cypress": "cypress run --browser chrome --headless", | ||
"jest:rc": "jest --findRelatedTests", | ||
"jest": "jest", | ||
"lint:rc": "eslint --fix", | ||
"lint": "eslint .", | ||
"prepublish": "npm run build", | ||
"prettier": "prettier --write '*.{js,md}'" | ||
"prettier:rc": "prettier --write", | ||
"prettier": "prettier --write .", | ||
"test": "npm run lint && npm run jest", | ||
"uglify": "uglifyjs dist/CancelablePromise.js -c -m -o dist/CancelablePromise.min.js" | ||
}, | ||
@@ -24,3 +39,3 @@ "repository": { | ||
"author": "Alkemics", | ||
"license": "GPL-2.0", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -34,8 +49,13 @@ "url": "https://github.com/alkemics/CancelablePromise/issues" | ||
"@babel/preset-env": "~7.9.0", | ||
"@babel/register": "~7.9.0", | ||
"chai": "~4.2.0", | ||
"babel-eslint": "~10.1.0", | ||
"core-js": "~3.6.4", | ||
"cypress": "~4.4.0", | ||
"eslint": "~6.8.0", | ||
"mocha": "~7.1.1", | ||
"prettier": "~2.0.4" | ||
"fs-extra": "~9.0.0", | ||
"husky": "~4.2.3", | ||
"jest": "~25.3.0", | ||
"lint-staged": "~10.1.2", | ||
"prettier": "~2.0.4", | ||
"uglify-js": "~3.9.1" | ||
} | ||
} |
174
README.md
@@ -1,5 +0,37 @@ | ||
# CancelablePromise | ||
# cancelable-promise | ||
A simple Cancelable Promise for browser | ||
[![GitHub license](https://img.shields.io/github/license/alkemics/CancelablePromise)](https://github.com/alkemics/CancelablePromise/blob/master/LICENSE) [![npm version](https://img.shields.io/npm/v/cancelable-promise)](https://www.npmjs.com/package/cancelable-promise) [![Node.js CI](https://github.com/alkemics/CancelablePromise/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/alkemics/CancelablePromise/actions?query=workflow%3A%22Node.js+CI%22) [![End-to-end tests](https://github.com/alkemics/CancelablePromise/workflows/End-to-end%20tests/badge.svg?branch=master)](https://github.com/alkemics/CancelablePromise/actions?query=workflow%3A%22End-to-end+tests%22) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/alkemics/CancelablePromise/pulls) | ||
A simple Cancelable Promise. | ||
This package is based on ES Promise. | ||
- See [caniuse](https://caniuse.com/#search=Promise) for browser support | ||
- See [core-js](https://github.com/zloirock/core-js#ecmascript-promise) for polyfills | ||
FYI, you can cancel a fetch request with AbortController & AbortSignal. | ||
- See [MDN](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) | ||
- See [caniuse](https://caniuse.com/#feat=abortcontroller) | ||
## Table of Contents | ||
- [Install](#install) | ||
- [Usage](#usage) | ||
- [Basic example](#basic-example) | ||
- [NodeJS](#nodejs) | ||
- [Browser](#browser) | ||
- [API](#api) | ||
- [cancelable](#cancelable) | ||
- [CancelablePromise](#cancelablepromise) | ||
- [Static methods](#static-methods) | ||
- [Scripts](#scripts) | ||
- [Build](#build) | ||
- [Tests](#tests) | ||
- [End-to-end tests](#end-to-end-tests) | ||
- [Contributing](#contributing) | ||
- [Contributors](#contributors) | ||
- [Code of conduct](#code-of-conduct) | ||
- [License](#license) | ||
## Install | ||
@@ -11,18 +43,138 @@ | ||
This package is based on ES6 Promise. See `promise-polyfill` for browser support. | ||
## Usage | ||
CancelablePromise acts like a ES6 Promise: you can use `Promise.all`, `Promise.race` with your CancelablePromise for example. The only difference is you'll have a `cancel` method on your promise to cancel future execution of `then` or `catch` functions. CancelablePromise will also cancel all callbacks attached to new promises returned by `then`/`catch`. | ||
CancelablePromise acts like an ES Promise: you can use `Promise.all`, `Promise.race` with your CancelablePromise for example. The only difference is you'll have a `cancel` method on your promise to cancel future execution of `then` or `catch` functions. CancelablePromise will also cancel all callbacks attached to new promises returned by `then`/`catch`. | ||
**A callback passed to `finally` will be always executed.** | ||
### Basic example | ||
```javascript | ||
import { cancelable, CancelablePromise } from 'cancelable-promise'; | ||
const promises = [ | ||
cancelable(new Promise((resolve) => setTimeout(resolve, 1))), | ||
new CancelablePromise((resolve) => setTimeout(resolve, 1)), | ||
]; | ||
for (const promise of promises) { | ||
promise.then(() => console.log('not logged')); | ||
promise.cancel(); | ||
} | ||
// Nothing will be logged | ||
``` | ||
### NodeJS | ||
```javascript | ||
const { cancelable } = require('cancelable-promise'); | ||
cancelable(new Promise((resolve) => resolve('ok'))); | ||
``` | ||
### Browser | ||
```html | ||
<script src="https://unpkg.com/cancelable-promise@3.0.0/dist/CancelablePromise.min.js"></script> | ||
<script> | ||
const { cancelable } = window.CancelablePromise; | ||
cancelable(new Promise((resolve) => resolve('ok'))); | ||
</script> | ||
``` | ||
## API | ||
### cancelable | ||
```javascript | ||
import { cancelable } from 'cancelable-promise'; | ||
/** | ||
* @param {Promise} arg - a native Promise | ||
* @returns {CancelablePromise} | ||
*/ | ||
cancelable( | ||
new Promise((resolve) => { | ||
resolve('ok'); | ||
}) | ||
); | ||
``` | ||
### CancelablePromise | ||
```javascript | ||
import CancelablePromise from 'cancelable-promise'; | ||
const myPromise = new CancelablePromise((resolve) => setTimeout(() => resolve('I\'m resolved'), 100)); | ||
myPromise.then((response) => console.log(response)).then(() => console.log('not cancel')); | ||
myPromise.cancel(); | ||
// Nothing will be displayed in console | ||
/** | ||
* @param {(resolve, reject) => void} arg - a promise executor | ||
* @returns {CancelablePromise} | ||
*/ | ||
new CancelablePromise((resolve, reject) => { | ||
resolve('ok'); | ||
}); | ||
``` | ||
## Test | ||
### Static methods | ||
You can run tests with `npm run test` | ||
Same as Promise static methods. | ||
```javascript | ||
import CancelablePromise from 'cancelable-promise'; | ||
CancelablePromise.resolve(); | ||
CancelablePromise.reject(); | ||
CancelablePromise.all([promise1, promise2]); | ||
CancelablePromise.race([promise1, promise2]); | ||
CancelablePromise.allSettled([promise1, promise2]); | ||
``` | ||
You can still use the native Promise API and wrap your promise: | ||
```javascript | ||
import { cancelable } from 'cancelable-promise'; | ||
cancelable(Promise.all([promise1, promise2])); | ||
cancelable(Promise.race([promise1, promise2])); | ||
cancelable(Promise.allSettled([promise1, promise2])); | ||
``` | ||
## Scripts | ||
### Build | ||
Run babel | ||
``` | ||
npm run build | ||
``` | ||
### Tests | ||
Run `eslint` and `jest` | ||
```shell | ||
npm test | ||
``` | ||
### End-to-end tests | ||
Run `cypress` | ||
```shell | ||
npm run cypress | ||
``` | ||
## Contributing | ||
Feel free to dive in! [Open an issue](https://github.com/alkemics/CancelablePromise/issues) or [submit PRs](https://github.com/alkemics/CancelablePromise/compare). | ||
### Contributors | ||
This project exists thanks to all [the people who contribute](https://github.com/alkemics/CancelablePromise/graphs/contributors). | ||
## Code of conduct | ||
[Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). | ||
## License | ||
[MIT License](LICENSE) © Alkemics |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
38012
10
0
100
679
180
13
1