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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@anlerandy/timer

v2.0.0-5

Published

Timeout manager for sensible process.

Readme

Timer v^2.0.0

Add a timer to a promise by wrapping it.

Install

$ npm i @anlerandy/timer

Usage

// ES6
import Timer from '@anlerandy/timer';
// CommonJS
var Timer = require('@anlerandy/timer').default;

Promise

const task = require('./my_asynchronous_function');
const Timer = require('@anlerandy/timer');

const promise = task();
// Your function has 60'000ms (1minute) to finish
const result = await new Timer(60000).launchTimer(promise);
  const task = require('./my_asynchronous_function');

  const promise = task();
  const result = await new Promise(function (resolve, reject) {
    const id = setTimeout(reject, 120000, 'Timeout');
    try {
      const result = await promise;
      resolve(result);
    } catch (error) {
      reject(error);
    }
    clearTimeout(id);
  })

Callback

const task = require('./my_function');
const Timer = require('@anlerandy/timer');

const timer = new Timer();

task(callback);
launchTimer(onFailure); // Launch clock and calls onFailure if time runs out

function callback(error) {
  timer.done(); // Stop the clock without calling onFailure
  if (error) {
    return onFailure();
  }
  // Do Something
}

function onFailure() {
  // Do Something
}

Cron

// toCronTask is async
const toCronTask = require('./my_function_to_cron');
const Timer = require('@anlerandy/timer');

const cronId = 'cron-id';

function start() {
  // Create and save cron. If aborted/done, will self destruct (destroy: true)
  Timer.getById(cronId, { createOne: true, destroy: true });
  return loop();
}

function stop() {
  const cron = Timer.getById(cronId);
  // Unless already stop, it should exist since `start` call
  if (cron) {
    // Will be destroyed after done
    cron.done();
  }
}

function loop() {
  toCronTask().finaly(() => {
    // It was created in `start`
    const cron = Timer.getById(cronId);
    // Unless `cron` is destroyed, repeat `loop`
    if (cron) {
      // You can also verify if accidently launched and keep it from launching twice
      // if (!cron.inProgress)
      cron.launchTimer(loop);
    }
  });
}

Instance properties

Only time property can be set (timer.time = 2000). Will not be updated if value is not a number.
Changing time while Timer is running can result in a timeout.

| Property | Type | Default | Description | | ------------- | :-------: | :---------: | ------------------------------------ | | _id | String | N/A | id of the instance | | createdAt | Date | N/A | Creation date | | startedAt | Date | undefined | Launch date | | lastUpdate | Date | createdAt | Last update (i.e. last postpone) | | inProgress | boolean | false | True if running | | isAborted | boolean | false | True if cancelled | | isSelfAborted | boolean | false | True if the timer cancelled its task | | time | Number | 120000 | Time to wait before timeout (ms) |

All properties are undefined or 0 if the instance is being deleted.

const timer = new Timer();
timer.destroy();
const date = timer.createdAt;
const time = timer.time;
console.log(date, time);
// output: undefined, 0

Instance API

timer.launchTimer(callback: function | promise, argument?)

Run the timer.
If callback argument is a function, it returns undefined.
If it's a Promise, it will return a Promise.

timer.launchOrUpdate(callback: function | promise, argument?)

Run the timer or update it if already inProgress.

timer.done()

Terminate timer without triggering callback or promise.reject.
If destroy option was true on instanciation, deletes the timer.

timer.destroy()

Destroy the instance.
Throw an error if instance is not terminated via _.done() or _.abort().
Automaticaly called after termination if destroy option was set as true.

timer.update()

Reset the clock of timer.
Useful if there is multiple task to be done, and each should be under the same timer.

timer.abort()

Run callback or promise.reject. Then, work as timer.done().
Can be used to terminate a task if the said task verify if timer is aborted.