Create a gist now

Instantly share code, notes, and snippets.

@1602 /proof.js
Created Oct 30, 2012

Some async pattern
var tags = ['tag1', 'tag2', 'tag3'];
(function oneIteration() {
var item = tags.shift();
if (item) {
doAsyncJob(item, oneIteration);
} else {
allDone();
}
})();
function allDone() {
console.log('all done');
}
function doAsyncJob(item, done) {
console.log('async', item);
setTimeout(done, 1000 * Math.random());
}
$ node proof.js
async tag1
async tag2
async tag3
all done
@tracker1

try it actually async...


...
function doAsyncJob(item, done) {
  //don't callback right away...
  process.nextTick(function(){
    console.log('async', item);
    setTimeout(done, 1000 * Math.random());
  });
}
@tracker1

Okay... nm.. didn't notice the recursive call in oneIteration...

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