Skip to content

Instantly share code, notes, and snippets.

@tankala
Last active February 11, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tankala/220f4ab4e8a1a7427b8abf022a2a5a26 to your computer and use it in GitHub Desktop.
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
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