time-limits

1.0.0 • Public • Published

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

Package Sidebar

Install

npm i time-limits

Weekly Downloads

3

Version

1.0.0

License

CC0-1.0

Unpacked Size

32.1 kB

Total Files

17

Last publish

Collaborators

  • nikolay_makhonin