Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created July 13, 2010 18:51
Show Gist options
  • Select an option

  • Save cowboy/474314 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/474314 to your computer and use it in GitHub Desktop.
Queuing article code idea
// I need to explain this. Soon.
// BAD
function a(){
console.log(1);
b();
};
function b(){
console.log(2);
setTimeout( c, 1000 );
};
function c(){
console.log(3);
console.log( 'done' );
};
a();
// GOOD
var queue = [
function(){
console.log(1);
},
function(){
console.log(2);
setTimeout( next, 1000 );
return false;
},
function(){
console.log(3);
}
];
function next(){
if ( queue.length ) {
if ( queue.shift()() !== false ) {
next();
}
} else {
console.log( 'done' );
}
}
next();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment