Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


How to use cargoQueue in Async

Educative Answers Team

cargoQueue() is a method used in concurrent programming with the Async module. cargoQueue() creates a cargoQueue object that can be used to manage tasks. We specify the payload and number of workersasynchronous functions that process given tasks to work in parallel on the provided tasks.

Tasks are added to the cargoQueue object and processed together (up to the payload limit). The parameter concurrency (see below) determines the number of parallel workers. If all workers are in progress, the task is queued until one becomes available. Once a worker has completed some tasks, each callback of those tasks is called.

Cargo and queue

cargoQueue is essentially a combination of the cargo and queue objects. The animations below show how cargo and queue work.

How cargo works
How cargo works
How queue works
How queue works

While queue only passes one task to one group of workers at a time, and cargo passes an array of tasks to a single worker, the cargoQueue passes an array of tasks to multiple parallel workers.


cargoQueue(worker, concurrency, payload)

  • worker: An asynchronous function for processing an array of queued tasks invoked with (tasks, callback).
  • concurrency: (Optional) The number of worker functions to run in parallel. The default value is 11.
  • payload: (Optional) The number of tasks to be processed per round. The default is unlimited.

Returns a cargoQueue object to manage the tasks. Callbacks can be attached as certain properties to listen for specific events during the lifecycle of the cargoQueue and inner queue.


In the following example, we create a cargoQueue object with concurrency=2 and payload=2. We pass an async function (worker) that takes an array of tasks and outputs each task’s name.

To add tasks into the cargoQueue we use cargoQueue.push().

import async from 'async';

// create a cargoQueue object with concurrency 2 and payload 2
var cargoQueue = async.cargoQueue(function(tasks, callback) {
    for (var i=0; i<tasks.length; i++) {
        console.log('hello ' + tasks[i].name);
}, 2, 2);

// add some items
cargoQueue.push({name: 'foo'}, function(err) {
    console.log('finished processing foo');
cargoQueue.push({name: 'bar'}, function(err) {
    console.log('finished processing bar');
cargoQueue.push({name: 'baz'}, function(err) {
    console.log('finished processing baz');
cargoQueue.push({name: 'boo'}, function(err) {
    console.log('finished processing boo');


Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring