Last active
July 11, 2016 22:41
-
-
Save mariyadiminsky/1a4264e135ecd59e2804c27542f0faa3 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
// non-blocking / async code: | |
var fs = require('fs'); | |
console.log('ALERT: Escape from bunny prison!'); | |
// notice the method is called readFile not readFileSync. | |
fs.readFile('wishlist.html', function(err, wishlist) { | |
if(err) throw err; | |
console.log(wishlist.toString()); | |
}) | |
console.log('ALERT: Pounce owner!'); | |
console.log('ALERT: Savagely devour rasberries!'); | |
/* EXTRA: This method adds something new to your wishlist.html file. | |
Run 'node bunnyAsync.js' and see for yourself. Notice I leave this | |
method in blocking format otherwise, if I left in as appendFile (the | |
async version), the fs.readFile reads the wishlist.html file async and | |
then all the ALERT's print before the appendFile method is done | |
processing. Thus you won't see the change taking place until you call | |
'node bunnyAsync.js' in your terminal again. | |
*/ | |
fs.appendFileSync('wishlist.html', '6. Shout "Happy with Async!!"', 'utf8'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment