Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
cancelable-promise
Advanced tools
A simple Cancelable Promise.
This package is based on ES Promise.
FYI, you can cancel a fetch request with AbortController & AbortSignal.
npm install --save cancelable-promise
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.
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
const { cancelable } = require('cancelable-promise');
cancelable(new Promise((resolve) => resolve('ok')));
<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>
import { cancelable } from 'cancelable-promise';
/**
* @param {Promise} arg - a native Promise
* @returns {CancelablePromise}
*/
cancelable(
new Promise((resolve) => {
resolve('ok');
})
);
import CancelablePromise from 'cancelable-promise';
/**
* @param {(resolve, reject) => void} arg - a promise executor
* @returns {CancelablePromise}
*/
new CancelablePromise((resolve, reject) => {
resolve('ok');
});
Same as Promise static methods.
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:
import { cancelable } from 'cancelable-promise';
cancelable(Promise.all([promise1, promise2]));
cancelable(Promise.race([promise1, promise2]));
cancelable(Promise.allSettled([promise1, promise2]));
Run babel
npm run build
Run eslint
and jest
npm test
Run cypress
npm run cypress
Feel free to dive in! Open an issue or submit PRs.
This project exists thanks to all the people who contribute.
Contributor Covenant Code of Conduct.
MIT License © Alkemics
3.0.0 (2020-04-07)
CancelablePromise
.
Now promises returned from Promise
API such as then
or catch
can cancel the root promise and all promises created from this root promise:// CancelablePromise v2
import CancelablePromise from 'cancelable-promise';
const promise = new CancelablePromise((resolve) => setTimeout(resolve, 1))
.then(() => console.log('callback2 executed'))
.then(() => console.log('callback3 executed'));
promise.cancel();
// logs:
// callback2 executed
// callback3 executed
// CancelablePromise v3
import CancelablePromise from 'cancelable-promise';
const promise = new CancelablePromise((resolve) => setTimeout(resolve, 1))
.then(() => console.log('callback2 executed'))
.then(() => console.log('callback3 executed'));
promise.cancel();
// no logs
import { cancelable } from 'cancelable-promise';
cancelable(new Promise((resolve) => setTimeout(resolve, 1)));
CancelablePromise
can be loaded in browserFAQs
A simple cancelable promise
The npm package cancelable-promise receives a total of 70,623 weekly downloads. As such, cancelable-promise popularity was classified as popular.
We found that cancelable-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.