provisor is simple JavaScript module that watch promises in specific namespace, with cancelable.
See the example.js to details.
var provisor = require('provisor');
function doAsync() {
return new Promise((resolve, reject) => {
setTimeout(resolve, 3000);
});
}
var request1 = doAsync();
var request2 = doAsync();
provisor.addNamespace('example');
// Make sure to use that returning object from provisor.save()
request1 = provisor.save('example', 'async1', request1);
request2 = provisor.save('example', 'async2', request2);
// request2 = provisor.save('example', 'async2', request2, { timeout: 1000 });
request1
.then(() => console.log('Done!'))
.catch((err) => console.log('Error', err));
request2
.then(() => console.log('Done!'))
.catch((err) => console.log('Error', err));
// Cancel example::async2
provisor.cancel('example', 'async2'); // comment this line when you want to test timeout
$ npm install --save provisor
var provisor = require('provisor');
...
// PROFIT!!!
This module not actually cancel the promise because it's not possible. So when you do some async job, and it ends after you cancel the promise, previous async job is still completed, but not it will not resolve the promise, because it's already did.
If you run the example, I canceled all promises, so the promise flow immediately ends, but timer is still going on, and process ends when timer is done.
Most of situations, it should be fine, especially in Web Development, but some cases, it could be a problem. So make sure that your situation is suitable for using this or not.
Returns provisor.
Returns all namespace. Namespace is just simple object.
Add new namespace.
Remove specified namespace. It will throw exception if namespace wasn't exists.
Returns true if specified namespace exists.
Save passed promise into specified namespace::key. You can access saved promise as "provisor.namespace.key". It returns new promise object that continue promise.
This method will remove promise from namespace::key automatically when it's done.
Forcely cancel specified namespace::key promise. Note that if you are using Fetch, using this just change the promise flow, not actually cancel XHR, it's limitation of Fetch API.
Forcely cancel all promises in specified namespace.
Returns new object that has same functions as provisor, but every namespace argument is binded automatically.
provisor.addNamespace('space1');
var iProvisor = provisor.use('space1');
var request = doAsync();
iProvisor.save('key1', request); // You can now skip namespace!
iProvisor.cancel('key1');
MIT. Free to use.