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

simple-timeout

v0.1.9

Published

Simple timeout for easy handling in Node.js developed in TypeScript.

Downloads

6

Readme

simple-timeout

Simple, intuitive, timeout library. Easy to use, not a simple promise wrapper, not overloaded with fancy utilities.

TypeScript version Node.js version MIT Build Status - GitHub Actions

Getting Started

npm install simple-timeout

Usage

// Initialization
const timeout = new Timeout(5000);

// On trigger
timeout.subscribe().then(() => {
  // your code here
})

// Clear
timeout.clear()

Timeout options

const timeout = new Timeout(5000, timeoutOptions)
  • timeoutMessage (optional) : System Out message when timeout is triggered.
  • callbackFn (optional): Callback function called when the timeout is triggered. status and timeoutMessage are accessible via the arguments of the callback function.
const timeout = new Timeout(5000, {
  callbackFn: ({ status, timeoutMessage }) => {
    // Your code here
  }
})

const timeout_two = new Timeout<number>(2000, (args) => (args.status === 'triggered') ? 1 : 2);
  • nullSubscription (optional): Setting it to true make the timeout.subscribe() return null when the timeout is cleared.

Note: When you provide the callbackFn in the timeoutOptions, it internally subscribes to the promise and provides the callback function to the then block. It is a shorthand for timeout.subscribe().then(() => {})

Timeout Status

Status = 'unset' | 'set' | 'cleared' | 'triggered'

console.log(timeout.status) // Gives the current status of the timeout 
  • unset is the default state.
  • Timeout status is switched to set when it is initialized.
  • triggeredis set when the timeout is triggered.
  • cleared is set when timeout is cleared.

Timeout Subscribe

timeout.subscribe()
  .then(({status, timeoutMessage}) => {
  // your code here
})

Returns a promise wrapper on setTimeout.

Timeout Clear

timeout.clear() 
// OR
timeout.clear(({status, timeoutMessage}) => {})

Clears the timeout. It takes an optional callback function as an argument.

Motivation

The idea for the library came about while I was working on a data gathering project that relies on scraping. For normal usage Puppeteer timeouts would suffice, but I kept on running into some advanced cases where I had to use native timeouts. Unfortunately, managing the native timeouts was a pain in the a** to deal with. Hence, this simple-timeout was the solution to the problem I was facing. I decided to separate it from the main project and make it into a library when I had other use cases for it. This library will be maintained as long as it's parent project is maintained.

PRs are welcome

Feel free to raise Issues and PRs addressing bugfixes. I don't plan for this project to be too bloated with features.

License

Licensed under the MIT. See the LICENSE file for details.