Created
May 15, 2016 05:57
-
-
Save Jeff-Russ/7396e21cf8b93b3695f45d65b4a351c7 to your computer and use it in GitHub Desktop.
Callback Cell II: The Return of Spaghetti Code
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
<!DOCTYPE html><html><head><script type="text/javascript"> | |
var i = 0; | |
function print(message){ console.log(i+' '+message) } | |
function main(callback){ | |
print('main') | |
i++; | |
if(i < 6) window.setTimeout( no_arg, 100); | |
else callback('I was passed to callback.'); | |
} | |
function no_arg(){ | |
print('no_arg'); | |
main(has_arg); | |
} | |
function has_arg(arg){ | |
print('has_arg'); | |
print(arg); | |
} | |
main(has_arg); | |
</script></head><body></body></html> | |
<!-- returns: | |
0 main | |
1 no_arg | |
1 main | |
2 no_arg | |
2 main | |
3 no_arg | |
3 main | |
4 no_arg | |
4 main | |
5 no_arg | |
5 main | |
6 has_arg | |
6 I was passed to callback. | |
--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment