Socket
Book a DemoInstallSign in
Socket

async-debounce-jt

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-debounce-jt

Guard against calling an asynchronous method multiple times whilst the asynchronous operation is in progress.

latest
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

Async Debounce

Guard against calling an asynchronous function multiple times whilst the asynchronous operation is in progress. Once the operation is complete, the last call made to the function whilst the operation was in progress is made.

Intermediate calls are lost, if no calls are made whilst the asynchronous operation is in progress the function is not called again.

This debounce is not based on time, it's based on a callback which is made once the operation is completed.

Usage

Installation

    npm install async-debounce

Invocation

    var debounce = require('async-debounce');
    
    var guarded = debounce(function (args, callback) {
        // do something with the arguments, then call the callback
    });
    
    guarded('hello', 'world'); // Parameters are combined in args parameter in the function
    

Example

This example calls our method every 10th of a second, but it takes 1 second to complete. The result is the guarded method is only called 10 times.

Code

    var debounce = require('async-debounce');
    
    var dostuff = debounce(function (args, callback) {
        setTimeout(function () {
            console.log('Called with: ', args[0]);
            callback();
        }, 1000);
    });
    
    var i = 0;
    var interval = setInterval(function () {
        dostuff(i);
        i+=1;
    }, 100);
    
    setTimeout(function () {
        clearInterval(interval);
    }, 10000)
        

Result

Called with:  0
Called with:  9
Called with:  20
Called with:  30
Called with:  40
Called with:  50
Called with:  60
Called with:  70
Called with:  80
Called with:  90
Called with:  98

Keywords

async

FAQs

Package last updated on 23 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.