Async.queue creates a queue object with the specified concurrency. Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task’s callback is called.
Let’s generate 10 unique tasks to add to our queue:
var _ = require('lodash');
//Generate an array of 10 random tasks;
var tasksList = _.times(10, _.uniqueId.bind(null, 'task_'));
Next, we define the tasksQueue array via async.queue:
var tasksQueue = async.queue(function (task, callback) {
console.log('Performing task: ' + task.name);
console.log('Waiting to be processed: ', tasksQueue.length());
console.log('----------------------------------');
//Simulate intensive processing
setTimeout(function() {
// If you want to pass an error object here, it will be caught in the task handler
// callback('something went wrong');
callback();
}, 1000);
}, 5);
We’re logging the task name via task.name, and we check the remaining tasks by calling tasksQueue.length(), we’re also setting a delay of 1 second to simulate intensive processing. The second parameter of 5 means we’re processing 5 of them at a time in parallel.
We then push the tasks into the queue:
_.each(tasksList, function(task) {
tasksQueue.push({name: task}, function(err) {
//Done
if (err) {
console.log(err);
}
});
});
Note that tasksQueue is just an array, so you can do standard array operations on it, such as pushing something important to the front of the queue:
//Puts a tasks in front of the queue
tasksQueue.unshift({name: 'Most important task'}, function(err) {
//Done
if (err) {
console.log(err);
}
});
When everything are processed, you can invoke the .drain() function as a callback:
// When all is processed, drain is called
tasksQueue.drain = function() {
console.log('all items have been processed.');
};
async.queue is very powerful and when used in the right context, it can manage your tasks in a clear and clean fashion.
That is it for our quick intro to some of the most useful functions from async.js Visit the Youtube playlist for a list of other not so common but useful functions:
If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!