Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
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
.
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, onCancel) => void} arg - an augmented promise executor
* @returns {CancelablePromise}
*/
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
onCancel
callback is working as in p-cancelable
/**
* @returns {void}
*/
cancelablePromise.cancel();
/**
* @returns {boolean}
*/
cancelablePromise.isCanceled();
/**
* @param {() => void} onFinally callback
* @param {boolean} runWhenCanceled force finally execution on cancel
* @returns {void}
*/
cancelablePromise.finally(() => {});
// You can release prematurely resources for a long running task
// by forcing finnaly callback execution when cancelling a promise
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();
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
FAQs
A simple cancelable promise
The npm package cancelable-promise receives a total of 4,778 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.