Skip to content

Instantly share code, notes, and snippets.

@tankala
Last active February 11, 2019 07:40
Show Gist options
  • Select an option

  • Save tankala/220f4ab4e8a1a7427b8abf022a2a5a26 to your computer and use it in GitHub Desktop.

Select an option

Save tankala/220f4ab4e8a1a7427b8abf022a2a5a26 to your computer and use it in GitHub Desktop.
Async Queue Example
const async = require('async');
//Code for processing the task
var processQueue = function (message, callback) {
setTimeout(function() {
console.log(`Task ${message} completed`);
callback();
}, 500);
}
//Queue initialization. This queue process 3 tasks at a time
var queue = async.queue(processQueue, 3);
//After all tasks completion queue process this function
queue.drain = function() {
console.log('Yuppie all tasks completed');
}
//To add tasks to queue we are using this function.
var processTasks = function () {
for (let index = 1; index <= 10; index++) {
queue.push(index);
}
}
processTasks();
@tankala
Copy link
Copy Markdown
Author

tankala commented Feb 11, 2019

Before running this example don't forget to install the Async package. To install it use below command

npm install async --save

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment