slow

rate-limit an async function

Usage no npm install needed!

<script type="module">
  import slow from 'https://cdn.skypack.dev/slow';
</script>

README

keep your pants on, javascript
npm install slow

Run a function in parallel, without going too fast.

Give it an array, and a function that returns a Promise.

then it tells you when it's done.

useful for courteous use of a web-service, or avoiding a blown-stack.

const slow = require('slow')

const randomWait = i => {
  return new Promise(resolve => {
    setTimeout(() => resolve(i), Math.random() * 3000)
  })
}
slow.walk([1, 2, 3, 4, 5, 6], randomWait).then(res => {
  console.log('done!')
  console.log(res) //[1,2,3,4,5,6]
})

or, if you prefer, as async/await

;(async () => {
  let res = await slow.walk(['larry', 'curly', 'moe'], randomWait)
  // ['larry', 'curly', 'moe']
})()

results are always in order.

one bad async call will not throw the whole operation, either.

Methods:

  • slow.crawl(arr, fn) - max 3
  • slow.walk(arr, fn) - max 5
  • slow.run(arr, fn) - max 10
  • slow.sprint(arr, fn) - max 15
  • slow.one(arr, fn) - max 1
  • slow.two(arr, fn) - max 2

In the browser:

<script src="https://unpkg.com/slow"></script>
<script defer>
  let urls = ['New_York_Yankees', 'Toronto_Blue_Jays', 'Boston_Red_Sox']
  slow.walk(urls, fetch).then(pages => {
    console.log(pages)
  })
</script>

See also