https://github.com/spencermountain/slow
whoa easy there javascript
https://github.com/spencermountain/slow
async control-flow maplimit
Last synced: over 1 year ago
JSON representation
whoa easy there javascript
- Host: GitHub
- URL: https://github.com/spencermountain/slow
- Owner: spencermountain
- Created: 2012-10-11T16:54:26.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:42:29.000Z (over 3 years ago)
- Last Synced: 2024-04-26T10:20:17.217Z (about 2 years ago)
- Topics: async, control-flow, maplimit
- Language: JavaScript
- Homepage:
- Size: 254 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
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.
```js
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`
```js
;(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:
```html
let urls = ['New_York_Yankees', 'Toronto_Blue_Jays', 'Boston_Red_Sox']
slow.walk(urls, fetch).then(pages => {
console.log(pages)
})
```
### See also
- [async/maplimit](https://caolan.github.io/async/docs.html#mapLimit)
- [dbrockman/promise-map-limit](https://github.com/dbrockman/promise-map-limit)
- [addaleax/promise-ratelimit](https://github.com/addaleax/promise-ratelimit)
MIT