Created
July 4, 2012 17:08
-
-
Save jqtrde/3048364 to your computer and use it in GitHub Desktop.
Async/Sync JS example
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
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); | |
}); |
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took me a very long time to understand callbacks. This is what made it clear to me.