项目作者: szchenghuang

项目描述 :
A debounce function that delays invoking asynchronous functions.
高级语言: JavaScript
项目地址: git://github.com/szchenghuang/debounce-async.git
创建时间: 2016-09-20T07:24:19Z
项目社区:https://github.com/szchenghuang/debounce-async

开源协议:MIT License

下载


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.

  1. seconds elapsed 0 1 2 3 4
  2. d called - d d d d d - - - - - d d d d - - d - - - - -
  3. x x x x | x x x x |
  4. f called +-------> f +-------> f
  5. | |
  6. 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

  1. npm install debounce-async --save

Usage

  1. var debounce = require( 'debounce-async' );
  2. /**
  3. * debounce(func, [wait=0], [options={}])
  4. *
  5. * @param {Function} func The function to debounce.
  6. * @param {number} [wait=0] The number of milliseconds to delay.
  7. * @param {Object} [options={}] The options object.
  8. * @param {boolean} [options.leading=false] Specify invoking on the leading edge of the timeout.
  9. * @param {cancelObj} [options.cancelObj='canceled'] Specify the error object to be rejected.
  10. * @returns {Function} Returns the new debounced function.
  11. */

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

Example

Promise

  1. var debounce = require( 'debounce-async' );
  2. var f = value => new Promise( resolve => setTimeout( () => resolve( value ), 50 ) );
  3. var debounced = debounce( f, 100 );
  4. var promises = [ 'foo', 'bar' ].map( debounced );
  5. promises.forEach( promise => {
  6. promise
  7. .then( res => console.log( 'resolved:', res ) )
  8. .catch( err => console.log( 'rejected:', err ) )
  9. });
  10. // Output:
  11. // rejected: canceled
  12. // 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.

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

Same output can be expected.

Test

  1. npm test

License

MIT. See LICENSE.md for details.