keue v0.3.0
keue
Asynchronous tasks orchestration
Install
You can install the latest version of the package using npm:
$ npm install --save keueUsage
//Import package
var keue = require("keue");
//Generate a new instance
var tasks = new keue();
//Register a new sync task
tasks.addTask("task1", function (done) {
//Do some stuff
//...
//Call the done method when the task is completed
return done();
});
//Register a new async task
tasks.addTask("task2", function (done) {
//Call an asynchronous function
return myAsyncFunction(function (error) {
if (error) {
//If something went wrong, you can stop the tasks execution
//calling done method with an error object
return done(error);
}
//Do some stuff
//...
//Task completed
return done();
});
});
//Finish listener
k.on("finish", function () {
//Tasks finished successfully
//...
});
//Task error listener
k.on("error", function (error) {
//Error running a task
});
//Run the tasks
tasks.run("task1", "task2");API
var tasks = new keue();
Initialize tasks manager.
var tasks = new keue();tasks.addTask(name[, dependencies], handler);
Register a new task called name. You can optionally provide list of dependencies tasks to be executed and completed before this task is executed.
The last argument should be a function that will be called with the following arguments:
done: a function that should be called when the task is completed. If you call this function with anErrorobject, the tasks queue will be finished and theerrorevent will be triggered.
//Add a new task to the queue
tasks.addTask("task1", function(done) {
//If something went wrong running the queue, you can abort it
//by calling the next function with an error object
if(/* something went wrong */) {
//Abort the task
return done(new Error("Something went wrong"));
} else {
//Task completed without error
return done();
}
});
//Add a new task with dependencies
tasks.addTask("task2", ["task1"], function(done) {
//task1 will be completed before this task is executed
//Do your magic
//...
});tasks.removeTask(name)
Removes the task called name from the tasks list (if exists).
tasks.removeTask("task4");tasks.run(tasks...);
Start running the list of provided tasks. This methods accepts a string or an array of strings with the names of the tasks to run. If no arguments ar passed, all tasks will be executed. The tasks will be executed in the order that you provide to the tasks.run method. For example, if you have three tasks (task1, task2, and task3) and you want to execute them in descendant order:
tasks.run("task3", "task2", "task1"); //First will be executed "task3", then "task2" and last "task1".Or:
tasks.run(["task3", "task2", "task1"]);This method will fire the start event.
tasks.on(eventName, eventListener)
Register the eventListener function as a listener of the eventName event.
event start
The tasks queue started.
tasks.on("start", function () {
//The task queue started
});event finish
This event will be fired when the tasks queue finished successfully.
tasks.on("finish", function () {
//Tasks finished
});event error
Fired when the tasks queue was aborted due to a task error.
tasks.on("error", function (error) {
//Tasks aborted
});event task:start
Fired when a task was started.
tasks.on("task:start", function (name) {
console.log("Task " + name + " started");
});event task:end
Fired when a task was completed without error.
tasks.on("task:end", function (name) {
console.log("Task " + name + " completed!");
});License
MIT © Josemi Juanes.