JSFiddle - React, Tailwind, and code Playground
by stevenkaspar
HTML
<script>
var x = 45;
var y = 80;
var run = function(){ // y is not in the scope of the run function so we need to pass it in
console.log('could be 80 or 85(depending on race condition): ' + y);
console.log('should be 45: ' + x);
};
function apple(cb_fn){
var y = 60; // you can declar var y again to have another ignore global y
setTimeout(function(){
console.log('should be 60: ' + y);
cb_fn(); // this would be a callback function
}, 20);
}
apple(run); // you need to pass run in as a "callback" function
console.log('should be 45: ' + x);
y = 85;
console.log('should be 85: ' + y);
</script>