promise-all-limit

1.0.0 • Public • Published

996.icu

promise-all-limit

nodejs, limit the maximum number of concurrent tasks

npm install promise-all-limit
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

Readme

Keywords

Package Sidebar

Install

npm i promise-all-limit

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

5.66 kB

Total Files

7

Last publish

Collaborators

  • zengming00