impatient
v1.0.1
Published
A tool for turning asynchronous things into impatient versions of themselves
Downloads
1
Readme
impatient
A tool for turning asynchronous things into impatient versions of themselves
Install
npm installimpatient
Usage
var impatient = require('impatient');
var sleep = require('sleep-promise');
// impatient promise (wait for 1000ms but reject at 50ms)
impatient(sleep(1000), 50 /* max patience */).catch(function(err) {
console.error('promise rejection', err); // timeout error
});
// impatient callback (wait for 1000ms but give up at 50ms)
var impatientCb = impatient(function(cb) { setTimeout(cb, 1000); }, 50 /*max patience*/);
impatientCb(function(err) {
console.error('callback error', err); // timeout error
});
If you want to perform some cleanup on aborting, you may pass in a function as the 3rd param to impatient.
impatient(sleep(1000), 50, function() {
console.log('cleanup');
// return optional promise, which will delay the resolution till cleanup is done
}).catch(function(err) {
// cancels the wait with Error('timeout')
console.error(err);
});
var impatientCb = impatient(function(cb) { setTimeout(cb, 1000); }, 50, function(cb) {
console.log('cleaning up cb');
cb();
});
impatientCb(function(err) {
console.error('callback error', err);
});
License
MIT © Allain Lalonde