Skip to content

Parallelize function execution up to a defined limit.

License

Notifications You must be signed in to change notification settings

anders94/throttle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

throttle

Parallelize function execution in Node.js up to a defined limit.

Usually you either run functions one after the other sequentially or in parallel simultaneously. However, many times it would be more helpful to run up to some number of parallelized functions but fall back to serial beyond that. This Node.js module provides a framework for this.

Install

npm i @anders94/throttle

Usage

const Throttle = require('@anders94/throttle');

const throttle = new Throttle(2);

// wrap setTimeout in a promise
const timeout = (ms) => {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const wait = async (delay) => {
    console.log('start', delay, 'ms timer.');
    await timeout(delay);
    console.log('end', delay, 'ms timer.');

}

throttle.enqueue(async () => await wait(400));
throttle.enqueue(async () => await wait(300));
throttle.enqueue(async () => await wait(200));
throttle.enqueue(async () => await wait(100));

Output:

start 400 ms timer.
start 300 ms timer.
end 300 ms timer.
start 200 ms timer.
end 400 ms timer.
start 100 ms timer.
end 100 ms timer.
end 200 ms timer.

About

Parallelize function execution up to a defined limit.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published