Skip to content

kolodny/w8

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

w8

Gitter NPM version Build status Test coverage Downloads

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');
  }
});

About

Give promises and thunks timeouts

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published