Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. // filereader.js
  2. // 2015 cloudbender
  3.  
  4.  
  5. var fs = require('fs');
  6.  
  7.  
  8. (function main() {
  9.  
  10. console.log('main: started');
  11.  
  12.  
  13. // the second argument to readFile is a function, to be
  14. // called with an error or a data argument
  15. // when readFile is completed or has an error.
  16.  
  17. // readFile will schedule the callback and arguments, onto the event loop
  18. // to be called when node can get to it.
  19.  
  20. var myfile = fs.readFile('myfile.txt', function(err,data){
  21.  
  22. // we are now inside the called-back function
  23.  
  24. if (err) {
  25. console.log('error reading myfile.txt');
  26. return;
  27. }
  28. if (data) {
  29. console.log('myfile.txt contains data and had no error');
  30. return;
  31. }
  32.  
  33. });
  34.  
  35. console.log('main: completed');
  36. // main will complete before readFile completes.
  37. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement