Skip to content

Instantly share code, notes, and snippets.

@marksteward
Created January 27, 2013 22:47
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 marksteward/4651079 to your computer and use it in GitHub Desktop.
Save marksteward/4651079 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var async = require('async');
var q = async.queue(function (task, callback){
timelog('pop');
fs.appendFile('UIDs.txt', task.value + '\n', function (err) {
timelog('continue');
callback();
});
}, 250);
function millis() {
return new Date().getTime();
}
function timelog(msg) {
var a = [].slice.call(arguments);
msg = '%s: ' + msg;
a.splice(0, 1, msg, millis());
console.log.apply(this, a);
}
function wait(ms) {
var now = millis();
while (millis() - now < wait) {}
}
/* same function two ways */
function fillSync() {
timelog('push-sync');
q.push({'value': i++}, function(err) {});
if (i == 10) syncComplete = true;
}
function fillAsync() {
timelog('push-async');
q.push({'value': i++}, function(err) {});
if (i == 20) clearInterval(ival);
}
/* synchronous */
var i = 0;
var syncComplete = false;
while(!syncComplete) {
fillSync();
wait(1000);
}
/* asynchronous */
var ival = setInterval(fillAsync, 1000);
timelog('complete');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment