All Projects → szchenghuang → debounce-async

szchenghuang / debounce-async

Licence: MIT License
A debounce function that delays invoking asynchronous functions.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to debounce-async

P Map
Map over promises concurrently
Stars: ✭ 639 (+2942.86%)
Mutual labels:  promise, async-await
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (+580.95%)
Mutual labels:  promise, async-await
Then
🎬 Tame async code with battle-tested promises
Stars: ✭ 908 (+4223.81%)
Mutual labels:  promise, async-await
Basic Ftp
FTP client for Node.js, supports FTPS over TLS, passive mode over IPv6, async/await, and Typescript.
Stars: ✭ 441 (+2000%)
Mutual labels:  promise, async-await
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (+0%)
Mutual labels:  promise, async-await
Thunks
A small and magical composer for all JavaScript asynchronous.
Stars: ✭ 523 (+2390.48%)
Mutual labels:  promise, async-await
Rubico
[a]synchronous functional programming
Stars: ✭ 133 (+533.33%)
Mutual labels:  promise, async-await
Evt
💧EventEmitter's typesafe replacement
Stars: ✭ 305 (+1352.38%)
Mutual labels:  promise, async-await
Await Of
await wrapper for easier errors handling without try-catch
Stars: ✭ 240 (+1042.86%)
Mutual labels:  promise, async-await
Kitsu
🦊 A simple, lightweight & framework agnostic JSON:API client
Stars: ✭ 166 (+690.48%)
Mutual labels:  promise, async-await
P Iteration
Utilities that make array iteration easy when using async/await or Promises
Stars: ✭ 337 (+1504.76%)
Mutual labels:  promise, async-await
of
🍬 Promise wrapper with sugar 🍬
Stars: ✭ 13 (-38.1%)
Mutual labels:  promise, async-await
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+17895.24%)
Mutual labels:  promise, async-await
Posterus
Composable async primitives with cancelation, control over scheduling, and coroutines. Superior replacement for JS Promises.
Stars: ✭ 536 (+2452.38%)
Mutual labels:  promise, async-await
Express Promise Router
A lightweight wrapper for Express 4's Router that allows middleware to return promises
Stars: ✭ 309 (+1371.43%)
Mutual labels:  promise, async-await
Await Handler
Basic wrapper for await that allows handling of errors without try/catch blocks
Stars: ✭ 13 (-38.1%)
Mutual labels:  promise, async-await
arraync
Async Array methods polyfills
Stars: ✭ 16 (-23.81%)
Mutual labels:  promise, async-await
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (+4.76%)
Mutual labels:  promise, async-await
Foy
A simple, light-weight and modern task runner for general purpose.
Stars: ✭ 157 (+647.62%)
Mutual labels:  promise, async-await
conquerant
lightweight async/await for Clojure
Stars: ✭ 31 (+47.62%)
Mutual labels:  promise, async-await

debounce-async

Build Status NPM Package Dependency status devDependency status

NPM

A debounced function that delays invoking asynchronous functions.

Preliminaries

A debounced function groups sequential calls to a function within a period. Only the last call in the group is executed. The others are simply ignored with rejections as if no calls to them ever happened.

Say d is a debounced function of f, var d = debounce(f, 1000);, where f is an asynchronous function that returns a promise being resolved in 400ms since it gets called. Below is a depiction of a sequence of calls to d.

seconds elapsed    0         1         2         3         4
d called           - d d d d d - - - - - d d d d - - d - - - - -
                     x x x x |           x x x x     |
f called                     +-------> f             +-------> f
                                       |                       |
promise resolved                       +-> *                   +-> *

d denotes a call to function d, f denotes a call to function f, and * denotes a promise returned by f is resolved. x denotes a call to d returns a rejected promise.

Installation

npm install debounce-async --save

Usage

var debounce = require( 'debounce-async' );

/**
  * debounce(func, [wait=0], [options={}])
  *
  * @param {Function} func The function to debounce.
  * @param {number} [wait=0] The number of milliseconds to delay.
  * @param {Object} [options={}] The options object.
  * @param {boolean} [options.leading=false] Specify invoking on the leading edge of the timeout.
  * @param {cancelObj} [options.cancelObj='canceled'] Specify the error object to be rejected.
  * @returns {Function} Returns the new debounced function.
  */

This package aims at maintaining the same signature of the debounce function from lodash. Please report if there is discrenency.

Example

Promise

var debounce = require( 'debounce-async' );

var f = value => new Promise( resolve => setTimeout( () => resolve( value ), 50 ) );
var debounced = debounce( f, 100 );

var promises = [ 'foo', 'bar' ].map( debounced );
promises.forEach( promise => {
  promise
    .then( res => console.log( 'resolved:', res ) )
    .catch( err => console.log( 'rejected:', err ) )
});

// Output:
// rejected: canceled
// resolved: bar

In the example above, f is an asynchronous function which returns a promise. The promise is resolved with the input after 50ms. debounced is a debounced function of f with a delay of 100ms.

The debounced function is called twice consecutively by the callback of Array.proptotype.map, with 'foo' and 'bar' being the input value respectively. The two returned promises are next fullfilled by printing the resolved result or rejected error on the console.

This snippet results in the given output. The first promise was rejected while the second one was resolved. It is because the second call comes before the delay of 100ms since the first call fired.

async/await

Same thing when it comes to asynchronous ES7 async/await functions. Take the prior example and transform the f into an ES7 async function.

var f = async value => await new Promise( resolve => setTimeout( () => resolve( value ), 50 ) );

Same output can be expected.

Test

npm test

License

MIT. See LICENSE.md for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].