5.1.12 • Published 9 months ago

@tofandel/debounce-promise v5.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@tofandel/debounce-promise

test npm downloads js-standard-style license

NPM

Create a debounced version of a promise returning function

Install

npm i -S @tofandel/debounce-promise

Usage example

import debounce from '@tofandel/debounce-promise';

function expensiveOperation(value) {
  return Promise.resolve(value)
}

var saveCycles = debounce(expensiveOperation, 100);

[1, 2, 3, 4].forEach(num => {
  return saveCycles('call no #' + num).then(value => {
    console.log(value)
  })
})

// Will only call expensiveOperation once with argument `4` and print:
//=> call no #4
//=> call no #4
//=> call no #4
//=> call no #4

With leading=true

import debounce from '@tofandel/debounce-promise';

function expensiveOperation(value) {
  return Promise.resolve(value)
}

var saveCycles = debounce(expensiveOperation, 100, {leading: true});

[1, 2, 3, 4].forEach(num => {
  return saveCycles('call no #' + num).then(value => {
    console.log(value)
  })
})

//=> call no #1
//=> call no #4
//=> call no #4
//=> call no #4

With leading=true and trailing=false

import debounce from '@tofandel/debounce-promise';

function expensiveOperation(value) {
  return Promise.resolve(value)
}

var saveCycles = debounce(expensiveOperation, 100, {leading: true, trailing: false});

[1, 2, 3].forEach(num => {
  return saveCycles('call no #' + num).then(value => {
    console.log(value)
  })
})

//=> call no #1
//=> call no #1
//=> call no #1

With accumulate=true

import debounce from '@tofandel/debounce-promise';

function squareValues (argTuples) {
  return Promise.all(argTuples.map(args => args[0] * args[0]))
}

var square = debounce(squareValues, 100, {accumulate: true});

[1, 2, 3, 4].forEach(num => {
  return square(num).then(value => {
    console.log(value)
  })
})

//=> 1
//=> 4
//=> 9
//=> 16

Api

debounce(func, [wait=0], [{leading: true|false, trailing: true|false, accumulate: true|false})

Returns a debounced version of func that delays invoking until after wait milliseconds.

Set leading: true if you want to call func and return its promise immediately.

Set trailing: false if you don't want the original function to be called at the end after you called the debounced function and the wait time elapsed

Set accumulate: true if you want the debounced function to be called with an array of all the arguments received while waiting.

Supports passing a function as the wait parameter, which provides a way to lazily or dynamically define a wait timeout.

Example timeline illustration

function refresh() {
  return fetch('/my/api/something')
}
const debounced = debounce(refresh, 100)
time (ms) ->   0 ---  10  ---  50  ---  100 ---
-----------------------------------------------
debounced()    | --- P(1) --- P(1) --- P(1) ---
refresh()      | --------------------- P(1) ---
const debounced = debounce(refresh, 100, {leading: true})
time (ms) ->   0 ---  10  ---  50  ---  100 ---  110 ---
--------------------------------------------------------
debounced()    | --- P(1) --- P(2) --- P(2) --- P(2) ---
refresh()      | --- P(1) --------------------- P(2) ---
5.1.12

9 months ago

5.1.11

10 months ago

5.1.10

10 months ago

5.1.9

10 months ago

5.1.5

10 months ago

5.1.4

10 months ago

5.1.3

10 months ago

5.1.8

10 months ago

5.1.7

10 months ago

5.1.6

10 months ago

5.1.2

12 months ago

5.1.1

12 months ago

5.1.0

12 months ago

5.0.5

1 year ago

5.0.4

1 year ago

5.0.3

1 year ago

5.0.2

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.1.3

2 years ago

4.1.0

2 years ago

4.1.2

2 years ago

4.1.1

2 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

3.1.3

4 years ago

3.1.2

4 years ago