https://github.com/mapleincode/wm_each_limit
https://github.com/mapleincode/wm_each_limit
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mapleincode/wm_each_limit
- Owner: mapleincode
- License: mit
- Created: 2019-05-05T02:26:43.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-10T03:04:47.000Z (about 3 years ago)
- Last Synced: 2025-02-19T07:48:45.364Z (10 months ago)
- Language: JavaScript
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EachLimit
like async.eachLimit.for async/await or promise.
## Install
```bash
npm install vm-each-limit --save
```
## Usage
### 1. eachLimit
```javascript
const { eachLimit } = require('wm-each—limit');
function sleep(time) {
return new Promise(function(resolve) {
setTimeout(() => {
resolve();
}, time);
});
}
const done = async function(item) {
await sleep(3000);
return item * 2;
}
async function main() {
const items = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const results = await eachLimit(items, 2, done, { noerror: true, item => console.log(item.msg) });
console.log(results);
// should get [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
}
main()
.catch(err => console.log(err))
```
### 2. each & all & retry
```javascript
const { each, all, retry } = require('wm-each—limit');
const done = async function(item = 1) {
await sleep(3000);
return item * 2;
}
async function eachFunc() {
const items = [1, 2, 3, 4];
await each(items, done)
}
async function allFunc() {
await all([done, done, done]);
}
async function retryFunc() {
await retry(done, 3);
}
```