07 – Exploring Async.js – async.cargo

Source Code

In this tutorial, we’ll learn how to put your async functions on an assembly line and execute a set number of functions at a time.

var async = require('async');
var _ = require('lodash');

//Generate an array of 10 random tasks;
var tasksList = _.times(10, _.uniqueId.bind(null, 'task_'));

// Create a cargo object with payload 3
// cargo(worker, [payload])
var tasksCargo = async.cargo(function(tasks, callback) {
    for(var i=0; i<tasks.length; i++){
      console.log('working on ' + tasks[i].name);

    //Something goes wrong, this will pass error to the task function and it will error out.
}, 3);

//Push tasks to tasksList
_.each(tasksList, function(task) {
	tasksCargo.push({name: task}, function(err) {
		//Something went wrong
		if (err) {

		//All good here
		console.log('Task ' + task + ' is done');


Comments Or Questions? Discuss In Our Discord

If you enjoyed this tutorial, make sure to subscribe to our Youtube Channel and follow us on Twitter @pentacodevids for latest updates!

More from PentaCode