async-tasks let you run tasks asynchronously in a simple way with the ability to make dependencies between them.
$ npm install --save async-tasks
var asyncTasks = require('async-tasks');
var tasks = asyncTasks();
tasks
.do(function (args, index, done) {
setTimeout(function (){
console.log('task 1 is done');
done();
}, 2000);
})
.do(function (args, index, done) {
console.log('task 2 is done');
done();
})
.wait()
.do(function (args, index, done) {
console.log('task 3 is done after waiting for tasks 1 & 2 to finish');
done();
})
//start tasks 1 & 2 asynchronously and wait till they finish then continue and run task 3
.start(function () {
console.log('all tasks are done');
});
//task 2 is done
//task 1 is done
//task 3 is done after waiting for tasks 1 & 2 to finish
add a task to the tasks list
Type:function(args, index, done)
args: the passed argument on (asyncTask.do).
index: the function index (its order among tasks).
done: the function that needs to be called when the task is done.
Cause the tasks added after wait() to wait till the tasks before it finish.
Type: function
called on start waiting.
Type: function
called on finish waiting.
Note: consecutive wait called are squashed into one wait.
start added tasks asynchronously
The callback function, which is called when all tasks are done.
Type: function(error)
error: error returned by the running tasks if they encounter an error
MIT © Ahmed AlSahaf