https://github.com/zengming00/promise-all-limit
nodejs, limit the maximum number of concurrent tasks
https://github.com/zengming00/promise-all-limit
Last synced: about 1 year ago
JSON representation
nodejs, limit the maximum number of concurrent tasks
- Host: GitHub
- URL: https://github.com/zengming00/promise-all-limit
- Owner: zengming00
- License: mit
- Created: 2019-05-07T09:03:04.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-07T10:21:25.000Z (about 7 years ago)
- Last Synced: 2025-03-13T23:46:27.252Z (over 1 year ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
nodejs, limit the maximum number of concurrent tasks
```sh
npm install promise-all-limit
```
```js
const promiseAllLimit = require('promise-all-limit');
function job_reject_b(name) {
const text = `job ${name}`
console.log('started', text)
return new Promise(function (resolve, reject) {
setTimeout(() => {
if (name === 'b') {
return reject(new Error('b reject'));
}
console.log(' ', text, 'finished')
resolve(text);
}, 100)
})
}
async function test() {
try {
const jobs = ['a', 'b', 'c', 'd', 'e'];
const concurrency = 3;
const results = await promiseAllLimit(jobs, concurrency, function (item) {
return job_reject_b(item);
});
console.log('results:', results)
} catch (e) {
console.log('---> err:', e.message);
}
}
test();
```
will output:
```
started job a
started job b
started job c
job a finished
started job d
job c finished
started job e
---> err: b reject
job d finished
job e finished
```
# License
MIT