Last active
December 16, 2015 08:09
-
-
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…
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
// 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