cancelable-promise
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -1,3 +0,59 @@ | ||
## [4.0.0](https://github.com/alkemics/CancelablePromise/releases/tag/4.0.0) (2021-05-27) | ||
## [4.1.0](https://github.com/alkemics/CancelablePromise/releases/tag/v4.1.0) (2021-06-16) | ||
- Prepare release without commit | ||
- Cancel cancelable promise returned by a then/catch callback | ||
Consider this example: | ||
```js | ||
const { CancelablePromise } = require('cancelable-promise'); | ||
const promise1 = new CancelablePromise((resolve, reject, onCancel) => { | ||
const timer = setTimeout(() => { | ||
console.log('resolve promise1'); | ||
resolve(); | ||
}, 1000); | ||
const abort = () => { | ||
clearTimeout(timer); | ||
}; | ||
onCancel(abort); | ||
}); | ||
const promise2 = promise1.then(() => { | ||
const promise3 = new CancelablePromise((resolve, reject, onCancel) => { | ||
const timer = setTimeout(() => { | ||
console.log('resolve promise 3'); | ||
resolve(); | ||
}, 1000); | ||
const abort = () => { | ||
clearTimeout(timer); | ||
}; | ||
onCancel(abort); | ||
}); | ||
return promise3; | ||
}); | ||
setTimeout(() => { | ||
console.log('cancel promise 2'); | ||
promise2.cancel(); | ||
}, 1500); | ||
``` | ||
Before this release, output was: | ||
``` | ||
resolve promise1 | ||
cancel promise 2 | ||
resolve promise 3 | ||
``` | ||
Now if you return a cancelable promise in a then/catch callback, it will cancel it too when you are canceling the parent promise. Output will be: | ||
``` | ||
resolve promise1 | ||
cancel promise 2 | ||
``` | ||
## [4.0.0](https://github.com/alkemics/CancelablePromise/releases/tag/v4.0.0) (2021-05-27) | ||
- Update dependencies and add esm module | ||
@@ -7,3 +63,3 @@ | ||
No more `dist` folder, you will find releases in `umd` and `esm` fodlers. | ||
No more `dist` folder, you will find releases in `umd` and `esm` folders. | ||
@@ -10,0 +66,0 @@ ```diff |
{ | ||
"name": "cancelable-promise", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "A simple cancelable promise", | ||
@@ -42,4 +42,7 @@ "keywords": [ | ||
"prettier": "prettier --write .", | ||
"release:major:nc": "node scripts.js prepare_release major --no-commit", | ||
"release:major": "node scripts.js prepare_release major", | ||
"release:minor:nc": "node scripts.js prepare_release minor --no-commit", | ||
"release:minor": "node scripts.js prepare_release minor", | ||
"release:patch:nc": "node scripts.js prepare_release patch --no-commit", | ||
"release:patch": "node scripts.js prepare_release patch", | ||
@@ -46,0 +49,0 @@ "test:e2e": "concurrently 'npm run test:serve' 'wait-on http://localhost:3000/esm.html && npm run cypress' --kill-others --success first", |
@@ -5,3 +5,10 @@ function createCallback(onResult, options) { | ||
if (!options.isCanceled) { | ||
return onResult(arg); | ||
const result = onResult(arg); | ||
if (result && typeof result.cancel === 'function') { | ||
if (!options.onCancelList) { | ||
options.onCancelList = []; | ||
} | ||
options.onCancelList.push(result.cancel); | ||
} | ||
return result; | ||
} | ||
@@ -8,0 +15,0 @@ return arg; |
@@ -27,3 +27,13 @@ (function (global, factory) { | ||
if (!options.isCanceled) { | ||
return onResult(arg); | ||
var result = onResult(arg); | ||
if (result && typeof result.cancel === 'function') { | ||
if (!options.onCancelList) { | ||
options.onCancelList = []; | ||
} | ||
options.onCancelList.push(result.cancel); | ||
} | ||
return result; | ||
} | ||
@@ -30,0 +40,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(n,e){if("function"==typeof define&&define.amd)define(["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var t={exports:{}};e(t.exports),n.CancelablePromise=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(n){"use strict";function e(n,e){if(n)return function(t){return e.isCanceled?t:n(t)}}function t(n,i){var r={then:function(r,l){return t(n.then(e(r,i),e(l,i)),i)},catch:function(r){return t(n.catch(e(r,i)),i)},finally:function(e,r){return r&&(i.finallyList||(i.finallyList=[]),i.finallyList.push(e)),t(n.finally((function(){return r&&(i.finallyList=i.finallyList.filter((function(n){return n!==e}))),e()})),i)},cancel:function(){i.isCanceled=!0;for(var n=0,e=[i.onCancelList,i.finallyList];n<e.length;n++){var t=e[n];if(t)for(;t.length;){var r=t.shift();"function"==typeof r&&r()}}},isCanceled:function(){return!0===i.isCanceled}};return{then:r.then.bind(void 0),catch:r.catch.bind(void 0),finally:r.finally.bind(void 0),cancel:r.cancel.bind(void 0),isCanceled:r.isCanceled.bind(void 0)}}function i(n){return t(n,{})}function r(n){var e=[];return t(new Promise((function(t,i){return n(t,i,(function(n){e.push(n)}))})),{onCancelList:e})}Object.defineProperty(n,"__esModule",{value:!0}),n.cancelable=i,n.CancelablePromise=r,n.default=void 0,r.all=function(n){return i(Promise.all(n))},r.allSettled=function(n){return i(Promise.allSettled(n))},r.race=function(n){return i(Promise.race(n))},r.resolve=function(n){return i(Promise.resolve(n))},r.reject=function(n){return i(Promise.reject(n))};var l=r;n.default=l})); | ||
!function(n,e){if("function"==typeof define&&define.amd)define(["exports"],e);else if("undefined"!=typeof exports)e(exports);else{var t={exports:{}};e(t.exports),n.CancelablePromise=t.exports}}("undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:this,(function(n){"use strict";function e(n,e){if(n)return function(t){if(!e.isCanceled){var i=n(t);return i&&"function"==typeof i.cancel&&(e.onCancelList||(e.onCancelList=[]),e.onCancelList.push(i.cancel)),i}return t}}function t(n,i){var r={then:function(r,l){return t(n.then(e(r,i),e(l,i)),i)},catch:function(r){return t(n.catch(e(r,i)),i)},finally:function(e,r){return r&&(i.finallyList||(i.finallyList=[]),i.finallyList.push(e)),t(n.finally((function(){return r&&(i.finallyList=i.finallyList.filter((function(n){return n!==e}))),e()})),i)},cancel:function(){i.isCanceled=!0;for(var n=0,e=[i.onCancelList,i.finallyList];n<e.length;n++){var t=e[n];if(t)for(;t.length;){var r=t.shift();"function"==typeof r&&r()}}},isCanceled:function(){return!0===i.isCanceled}};return{then:r.then.bind(void 0),catch:r.catch.bind(void 0),finally:r.finally.bind(void 0),cancel:r.cancel.bind(void 0),isCanceled:r.isCanceled.bind(void 0)}}function i(n){return t(n,{})}function r(n){var e=[];return t(new Promise((function(t,i){return n(t,i,(function(n){e.push(n)}))})),{onCancelList:e})}Object.defineProperty(n,"__esModule",{value:!0}),n.cancelable=i,n.CancelablePromise=r,n.default=void 0,r.all=function(n){return i(Promise.all(n))},r.allSettled=function(n){return i(Promise.allSettled(n))},r.race=function(n){return i(Promise.race(n))},r.resolve=function(n){return i(Promise.resolve(n))},r.reject=function(n){return i(Promise.reject(n))};var l=r;n.default=l})); |
Sorry, the diff of this file is not supported yet
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
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
62467
1164
0