time-limits

Queue of async functions (using promises) with multiple time limits or time limits tree


Keywords
time, limit, queue, promise, async, asynchronous
License
CC0-1.0
Install
npm install time-limits@1.0.0

Documentation

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Description

Queue of async functions (using promises) with multiple time limits or time limits tree

Examples

let { TimeLimit, TimeLimits } = require('time-limits')

const timeLimit = new TimeLimit(
	5, // max active functions
	10000 // per 10000 milliseconds
)

const timeLimits = new TimeLimits( // combine time limits
	timeLimit,
	timeLimit, // if you add strict equal instances only one will be used
	new TimeLimit(10, 60 * 1000),
	// you can create time limits tree
	new TimeLimits(new TimeLimit(1, 10000), new TimeLimit(5, 5 * 60 * 10000))
)

// run functions
const tasks = []
for (let i = 0; i < 100; i++) {
	tasks.push(timeLimits.run(() => console.log(i)))
}

for (let i = 0; i < 100; i++) {
	tasks.push(timeLimits.run(async () => console.log(i)))
}

for (let i = 0; i < 100; i++) {
	tasks.push(timeLimits.run(() => new Promise(resolve => {
		setTimeout(() => {
			console.log(i)
			resolve()
		}, 100)
	})))
}

Promise
	.all(tasks)
	.then(() => {
		console.log('completed')
	});

// OR

(async () => {
	await Promise.all(tasks)
	console.log('completed')
})()

License

CC0-1.0