w8
v2.0.4
Published
give promises and thunks timeouts
Downloads
18
Readme
w8
Give promises (and thunks/objects/arrays) a timeout that it must finish before or else it gets rejected
Example
var w8 = require('w8');
var fastPromise = new Promise(function(res) {
setTimeout(function() {
res('ok1');
}, 10);
});
var slowPromise = new Promise(function(res) {
setTimeout(function() {
res('ok2');
}, 1000);
});
w8(100, fastPromise).then(function(data) {
console.log('fastPromise was fast enough');
console.log(data); // 'ok1'
return w8(100, slowPromise);
}).catch(function(err) {
console.log("slowPromisie wasn't fast enough");
});
Usage
First argument is the timeout in ms and the second is the promise/thunk/object/array, it returns a promise
Here's a sample usage with co
var w8 = require('w8');
var co = require('co');
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs'));
co(function *() {
var fileReadPromise = fs.readFileAsync('package.json');
try {
var pkgStr = yield w8(100, fileReadPromise).toString();
console.log('package.json contents:', pkgStr);
} catch (e) {
console.error('getting file took more than 100 ms');
}
});