Created
January 27, 2013 22:47
-
-
Save marksteward/4651079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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