Skip to content

Instantly share code, notes, and snippets.

@satbirdd
Last active August 29, 2015 14:00
Show Gist options
  • Save satbirdd/11280909 to your computer and use it in GitHub Desktop.
Save satbirdd/11280909 to your computer and use it in GitHub Desktop.
// 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