-
Notifications
You must be signed in to change notification settings - Fork 27
/
example.js
42 lines (34 loc) · 854 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const debounce = require('debounce-promise')
function expensiveOperation (value, delay) {
return Promise.resolve(value)
}
// Simple example
{
const saveCycles = debounce(expensiveOperation, 100);
[1, 2, 3, 4].forEach(num => {
return saveCycles('call no #' + num).then(value => {
console.log(value)
})
})
}
// With leading=true
{
const saveCycles = debounce(expensiveOperation, 100, {leading: true});
[1, 2, 3, 4].forEach(num => {
return saveCycles('call no #' + num).then(value => {
console.log(value)
})
})
}
// With accumulate=true
{
function squareValues (values) {
return Promise.all(values.map(val => val * val))
}
const square = debounce(squareValues, 100, {accumulate: true});
[1, 2, 3, 4].forEach(num => {
return square(num).then(value => {
console.log(value)
})
})
}