Last active
August 29, 2015 14:00
-
-
Save satbirdd/11280909 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// do_sync(); | |
// do_async(); | |
// async_mode(); | |
// function: setTimeout | |
// sync | |
function do_sync() { | |
var result = 0; | |
sync(function() { | |
result = 4; | |
}) | |
console.log('$$ do_sync outputs', result) | |
function sync(fn) { | |
fn(); | |
} | |
} | |
// async | |
function do_async() { | |
var result = 0; | |
sync(function() { | |
// throw "error"; | |
result = 4; | |
}) | |
console.log('$$ do_sync outputs', result) | |
function sync(fn) { | |
setTimeout(fn); | |
} | |
} | |
// gotcha: return value and error handling | |
// javascript async thread mode | |
function async_mode() { | |
console.time('async') | |
setTimeout(function() { | |
console.log('done!'); | |
console.timeEnd('async'); | |
}, 2000) | |
console.time('sum') | |
var sum = 0; | |
for (var i = 0; i < 1000000000; i++) { | |
sum += i; | |
} | |
console.timeEnd('sum') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment