1.1.1 • Published 3 years ago

easy-task-scheduler v1.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
3 years ago

Easy task scheduler

Async task scheduler

Install

npm install easy-task-scheduler

Usage

const TaskScheduler = require('easy-task-scheduler');
const MAX_TASK = 10;

const print = (m) => console.log(m)
const sleep = (time) => {
    return new Promise((r) => setTimeout(r, time));
}

const taskScheduler = new TaskScheduler(); // New instance

// Define your async task
const task = (id) => {
    return async () => {
        print(`Length of task is ${taskScheduler.getLength()}`);
        print(`${id} start`);
        await sleep(id * 100);
        print(`${id} finished`);
    }
}

const main = async () => {
    taskScheduler.setMaxTaskNum(MAX_TASK); // Set max num allowed to run at the same time
    
    for (let i=0; i<=20; ++i) {
        await taskScheduler.push(task(i)); // Push task to scheduler, it will control the task length to a specified threshold
    }

    await taskScheduler.finsh(); // Ensure that all async tasks are completed
};

main().then(_ => {
    print('Task have been finished!');
});
1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

5 years ago

1.0.0

5 years ago