Skip to content

Instantly share code, notes, and snippets.

@jqtrde
Created July 4, 2012 17:08
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 jqtrde/3048364 to your computer and use it in GitHub Desktop.
Save jqtrde/3048364 to your computer and use it in GitHub Desktop.
Async/Sync JS example
var fs = require('fs')
// So, callbacks. When readFile is complete, the callback is 'called'
// And we get our console.log without blocking whatever might be next.
var file = fs.readFile('./example', 'utf-8', function(err, contents){
if (err)
console.log('You iz dumb');
else
console.log(file);
});
var fs = require('fs')
// Sequential parsing of commands. Depending on the size of file
// This could keep the program locked up for quite a while.
var file = fs.readFileSync('./example', 'utf-8')
console.log(file);
@jqtrde
Copy link
Author

jqtrde commented Jul 4, 2012

Took me a very long time to understand callbacks. This is what made it clear to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment