Created
July 13, 2010 18:51
-
-
Save cowboy/474314 to your computer and use it in GitHub Desktop.
Queuing article code idea
This file contains hidden or 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
| // 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