npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timeoutable

v0.0.0

Published

Wrap timeouts around anything, to fail fast and/or keep user-facing code responsive.

Downloads

1

Readme

Timeoutable

A lightweight module written around the node callback-as-last-argument pattern, that interrupts if something is slow to respond.

What's this for?

You don't trust libraries you use, or the web services behind them to respond in a timely manner; and waiting potentially forever is worse than failing fast.

Your application uses several libraries communicating with different remote services, so their individual timeout options aren't adequate to fail fast.

You want to guard against callbacks being called more or less than once (once being the best number of times for a callback to be called)

Basic usage

var timeoutable = require('timeoutable');

timeoutable(2000, callback).run(function(callback){
  thing.thatMightTakeMoreThanTwoSeconds(callback);
});

If the thing that might take more than two seconds actually takes more than two seconds, callback is invoked and passed an instance of TimeoutableError as the first argument. TimeoutableError is exported, so you can decide how severe of an error a timeout is:

var timeoutable = require('timeoutable');
var TimeoutableError = timeoutable.TimeoutableError;

function callback(err, response) {
  if(err) {
    if(err instanceof TimeoutableError) {
      // idgaf, continue
    } else {
      return callback(err);
    }
  }
};

Less basic usage

Still not a lot to it.

var timeoutable = require('timeoutable');

timeoutable expects a timeout duration, in milliseconds, and a callback; and returns an object with a "run" method.

timeoutable(5000, function() { alert('this does nothing') });
timeoutable(5000, function() { alert('hello') }).run(function(){
  callback(); // this alerts "hello"
});

If you're just wrapping a function without any state, the above example holds.

Arguments are passed in as arrays:

timeoutable(100, function() {}).run(function(a, b, c) {
  console.log(a, b, c);
}, [ 1, 2, 3 ])
// logs 1, 2, 3

If the timeoutable thing is a property of an instance of a "class", you need to do this:

something = {
  state: 5,
  editState: function(newState, callback) {
    this.state = newState;
    callback()
  }
}
timeoutable(5000, function() { /* the callback */ })
  .run(something, "editState", [ 4 ])
// something.state is now 4

Bonus features

This module absorbs things that might normally cause errors (example: callback being called multiple times, & the callback calls res.send); it tracks them, but doesn't complain unless you configure it to. (I need to add a bit more detail to this.)

Yeah, also

Wrote this on a Sunday evening. Please open an issue, or a pull request, if you have ideas for improvements!