Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active December 16, 2015 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/5403872 to your computer and use it in GitHub Desktop.
Save barneycarroll/5403872 to your computer and use it in GitHub Desktop.
`setTimeout(fn, 0)` is a hack to defer calling `fn` til the synchronous stack has executed, and has many very useful applications — but there's a bit of cognitive overhead in ascertaining that `setTimeout` isn't being used as per its intended use case: this flummoxes novices especially, who may interpret this as a mistake. Is it worth abstractin…
// functions passed to later will only execute once the stack has resolved.
function later(fn){
return setTimeout(fn,0);
}
void function executionScope(){
var x = 0;
console.log('Log 1: ' + x);
++x;
later(function(){
console.log('Log 2: ' + x);
});
++x;
console.log('Log 3: ' + x);
++x;
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment