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

async-polling

v0.2.1

Published

An easy way to run reliable polling without messing with setTimeout.

Downloads

10,931

Readme

AsyncPolling

An easy way to run reliable polling without messing with setTimeout.

Here is an article explaining why using setInterval is discouraged, especially when dealing with asynchronous tasks.

Installation

In the browser

With bower:

bower install async-polling

Then include the script:

<script src="bower_components/async-polling/dist/async-polling.min.js"></script>
<script>
// Here you can use the AsyncPolling constructor.
</script>

In NodeJS

With npm:

npm install async-polling

Then require the module:

var AsyncPolling = require('async-polling');

Usage

Here is the basic usage:

AsyncPolling(function (end) {
    // Do whatever you want.
        
    // Then notify the polling when your job is done:
    end();
    // This will schedule the next call.
}, 3000).run();

You can also send a result to the end callback with the usual signature (error, result). Pass null as first argument when everythin is fine:

var polling = AsyncPolling(function (end) {
    someAsynchroneProcess(function (error, response) {
        if (error) {
            // Notify the error:
            end(error)
            return;
        }
        
        // Do something with the result.
        
        // Then send it to the listeners:
        end(null, result);
    });
}, 3000);

polling.on('error', function (error) {
    // The polling encountered an error, handle it here.
});
polling.on('result', function (result) {
    // The polling yielded some result, process it here.
});

polling.run(); // Let's start polling.

See also the demo script.

API

Create a polling

var polling = AsyncPolling(pollingFunc, delay);
  • pollingFunc(end): [function] The function to run periodically; takes a callback as parameter to notify the end of the process and possibly send a result. It will be bound to the polling object.
  • delay: [number(ms)|object] the delay between two calls of pollingFunc. If the type is not number, the .valueOf() method of the object will be called to retrieve the amount of milliseconds.

Run the polling

polling.run();

Stop the polling

polling.stop();

Since the polling function is bound to polling, one can call this.stop() from within the polling function:

AsyncPolling(function (end) {
    // Do some stuff
    
    // Here I want to stop the polling:
    this.stop();
    end();
}, 3000).run();

Listen to events

polling.on(eventName, listener);
  • eventName: The name of the event for which we register (run, start, error, result, end, schedule, stop).
  • listener: The function to call when the specified event occurs.