Skip to content

Instantly share code, notes, and snippets.

@Jeff-Russ
Created May 15, 2016 05:57
Show Gist options
  • Save Jeff-Russ/7396e21cf8b93b3695f45d65b4a351c7 to your computer and use it in GitHub Desktop.
Save Jeff-Russ/7396e21cf8b93b3695f45d65b4a351c7 to your computer and use it in GitHub Desktop.
Callback Cell II: The Return of Spaghetti Code
<!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