1.0.1 • Published 10 years ago

impatient v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 years ago

impatient

NPM version Build Status

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

1.0.1

10 years ago

1.0.0

10 years ago