Code Monkey home page Code Monkey logo

async-polling's Introduction

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.

async-polling's People

Contributors

cguille avatar

Watchers

James Cloos avatar HSIAO PO WEN avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.