1.0.0 • Published 5 years ago

promise-all-limit v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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