cadept
v2.1.0
Published
Callable Async DEPendency Task
Downloads
2
Readme
CADEPT ( Callable Async DEPendency Task ) is an asynchronous task class with dependency management features.
Getting Started
Install the package with NPM:
npm i cadept
And require()
it:
const Task = require('cadept');
or import
it as an ES module:
import Task from 'cadept';
Usage
// Create a task
const task = new Task((resolve, reject) => {
/* do stuff */
resolve(); // Finish
});
task(); // Run a task
A task instance is callable just like a function. And it runs asynchronously.
The called task returns a Promise object so you can hook on the task resolution (or rejection) like this:
const task = new Task((resolve, reject) => {
resolve('Task Complete.');
});
task().then(result => {
console.log(result); // output: 'Task Complete.'
});
If you prefer simpler syntax, you can also write like this:
const task = new Task(() => {
return 'Task Complete.';
});
task().then(result => {
console.log(result); // output: 'Task Complete.'
});
Adding task dependencies
A task can be associated with another tasks as its dependencies.
const task_X = new Task(resolve => {
console.log('task_X: OK');
resolve();
});
const task_Y = new Task(resolve => {
console.log('task_Y: OK');
resolve();
});
const task_Z = new Task(resolve => {
console.log('task_Z: OK');
resolve();
}, [task_X, task_Y]); // Dependencies
task_Z(); // Run task_Z
Output:
task_X: OK
task_Y: OK
task_Z: OK
The 2nd parameter of Task()
is an array of dependency tasks, which are task_X
and task_Y
in the above example. When the dependent task ( task_Z
) is called, it calls its dependencies in parallel, prior to execute its own function, awaiting all the dependencies are resolved.
Alternatively, you can also use depend()
to add dependencies to a task.
task_Z.depend(task_X, task_Y);
task_Z();
For more instructions, examples, and advanced usages, please see: 📘 Documentations
And also check the real-world example: p5-livesketch/index.js