Skip to content

Instantly share code, notes, and snippets.

@mrlannigan
Created October 14, 2012 15:16
Show Gist options
  • Save mrlannigan/3888877 to your computer and use it in GitHub Desktop.
Save mrlannigan/3888877 to your computer and use it in GitHub Desktop.
async queue callback test
var async = require('async'), concurrency = 1, i, timestart = Date.now();
var testQueue = async.queue(function(task, callback) {
var _runonce = false;
console.log('running number '+task.number+', time: '+(Date.now() - timestart));
var cb = function() {
if (_runonce) return;
/*
Uncomment this line to show the difference between running the callback
only once and multiple times.
*/
//_runonce = true;
callback();
}
setTimeout(function() {
if (task.number == 2 || task.number == 10) {
cb();
cb();
} else {
cb();
}
}, 1000);
}, concurrency);
testQueue.saturated = function() {
console.log('testQueue: is saturated');
}
testQueue.empty = function() {
console.log('testQueue: is now empty');
}
testQueue.drain = function() {
console.log('testQueue: has finished processing all queued tasks');
}
for (i = 0; i <= 20; i++) {
testQueue.push({number: i});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment