0.2.3 • Published 1 year ago

@shokai/async-throttle v0.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

async-throttle

make async/promise function execute only one at a time.

Usage

throttle

const asyncThrottle = require('@shokai/async-throttle')

const delay = msec => new Promise(resolve => setTimeout(resolve, msec))

async function countUp (n = 0) {
  for (let i = 0; i < 5; i++) {
    console.log(n + i)
    await delay(100)
  }
}
const throttledCountUp = asyncThrottle(countUp)

throttledCountUp(0) // run
throttledCountUp(10) // skip this
await throttledCountUp(20) // skip this, but wait for "throttledCountUp(0)" to finish

throttledCountUp(30) // run this

result

0
1
2
3
4
30
31
32
33
34

trailing

When the function being executed is finished, it is executed only once at the last.

const throttledCountUp = asyncThrottle(countUp, {trailing: true})

throttledCountUp(0) // run
throttledCountUp(10) // skip
throttledCountUp(20) // skip
throttledCountUp(30) // run, but wait for "throttledCountUp(0)" to finish

result

0
1
2
3
4
30
31
32
33
34
0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago