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

kicktock

v0.9.1

Published

Simple progress and completion events for batches of asynchronous tasks and Promises.

Downloads

5

Readme

KICKTOCK

A simple way of doing asynchronous callbacks and Promises in JS.

// 1. Setup the object. (Here we are firing progress every 2 seconds.)
var kicktock = require("kicktock")
var K = kicktock(2000)
K.on('progress', () => console.log(`${K.at} of ${K.total}`))

// 2. Wrap all the functions that get called asynchronously with `K`.
// This "adds" tasks to the kicktock.
var request = require('superagent')
var urls = ['http://httpbin.org/headers', 'http://httpbin.org/ip',
  'http://httpbin.org/delay/3', 'http://httpbin.org/uuid',
  'http://httpbin.org/user-agent']
urls.forEach(url => {
  request.get(url).set('Accept', 'application/json')
    .end(K((err, res) => {
      console.log(res.text)
    }))
})

// 3. Start the progress for the kicktock. (The callback will run when the
// tasks monitored by the kicktock are all complete.)
K.go(() => {
  console.log("Finished all requests.")
})

Installation

npm install --save kicktock

How It Works

The kicktock is a function that can wrap another function or Promise.

If the kicktock is given a function, it returns a new function. The new function notifies the kicktock when the wrapped function is finished running.

var K = kicktock()
fs.readFile(file1, 'utf8', K((err, data) => { ... })
fs.readFile(file2, 'utf8', K((err, data) => { ... })
K.go(() => console.log('DONE!'))

If the kicktock is given a Promise, it returns the Promise but will monitor it. You can mix Promises and asynchronous functions as well.

var K = kicktock()
K(request.get(url1).then(data => { ... })
K(request.get(url2).then(data => { ... })
K.go(() => console.log('DONE!'))

The go function is asynchronous as well---it will not wait for the kicktock to end. It will immediately return and then the function supplied to it will run when the kicktock does end.

If the go function is too confusing, you can separate it into an end handler.

K.on('end', () => console.log('DONE!'))
K.go()

You can also assign a progress event handler.

count.on('progress', () => {
  console.log(`Completed ${count.at} of ${count.total} tasks.`)
})

The at property tells you how many tasks are currently completed. The total property tells you how many tasks are currently queued.

PLEASE NOTE: Progress tracking is very loose---because all asynchronous functions are executed in parallel, many will finish before more have been queued. This means that percentages on many short-running tasks will stay close to 100%.

By default, the progress function will fire for every item. You can also use a timer if you supply a microsecond count when you create the kicktock.

Error Handling

One really nice feature of the kicktock is that you can log all errors from any monitored Promises or callbacks.

var K = kicktock()
K.on('error', e => console.log(e))

This will work automatically for Promises.

For callbacks, you'll need to manually throw the errors. (Any exceptions thrown inside the callback or by the caller will be caught by the kicktock.)

fs.readFile(file1, 'utf8', K((err, data) => {
  if (err) throw err
  console.log(data)
})

However, if the callback you are using is an error-first callback (the error is the first argument in the callback), then you can have kicktock automatically handle the error by using the errorFirst function.

fs.readFile(file1, 'utf8', K.errorFirst(data => {
  console.log(data)
})

Notice that, if you take this approach, the error argument is removed from the callback, since the kicktock is handling it now.