cancelable-promise
Advanced tools
Changelog
4.3.1 (2022-10-14)
Noticeable changes:
Other changes:
Changelog
4.1.0 (2021-06-16)
Consider this example:
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
Changelog
4.0.0 (2021-05-27)
[Breaking change]
No more dist
folder, you will find releases in umd
and esm
folders.
- https://unpkg.com/cancelable-promise@3.0.0/dist/CancelablePromise.min.js
+ https://unpkg.com/cancelable-promise@4.0.0/umd/CancelablePromise.min.js
+ https://unpkg.com/cancelable-promise@4.0.0/esm/CancelablePromise.min.js
[Feature] ESM module
<script type="module">
import { cancelable } from 'https://unpkg.com/cancelable-promise@4.0.0/esm/CancelablePromise.min.mjs';
</script>
Changelog
3.2.0 (2020-10-10)
import CancelablePromise from 'cancelable-promise';
const promise = new CancelablePromise((resolve, reject, onCancel) => {
const worker = new Worker('some-script.js');
onCancel(() => {
worker.terminate();
});
worker.onmessage = (event) => resolve(event.data);
worker.onerror = (error) => reject(error);
});
promise.cancel(); // It will execute the callback passed to onCancel
let worker;
const promise = cancelable(
new Promise((resolve, reject) => {
worker = new Worker('some-script.js');
worker.onmessage = (event) => {
resolve(event.data); // never executed
};
worker.onerror = (error) => {
reject(error); // never executed
};
})
)
.then(() => {
console.log('never logged');
})
.finally(
() => {
console.log('executed');
if (worker) {
worker.terminate();
worker = null;
}
},
// runWhenCanceled boolean
true
);
promise.cancel();