@shokai/async-throttle

0.2.3 • Public • Published

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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.2.347latest

Version History

VersionDownloads (Last 7 Days)Published
0.2.347
0.2.20
0.2.125
0.2.00
0.1.00

Package Sidebar

Install

npm i @shokai/async-throttle

Weekly Downloads

72

Version

0.2.3

License

MIT

Unpacked Size

3.71 kB

Total Files

4

Last publish

Collaborators

  • shokai