Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // filereader.js
- // 2015 cloudbender
- var fs = require('fs');
- (function main() {
- console.log('main: started');
- // the second argument to readFile is a function, to be
- // called with an error or a data argument
- // when readFile is completed or has an error.
- // readFile will schedule the callback and arguments, onto the event loop
- // to be called when node can get to it.
- var myfile = fs.readFile('myfile.txt', function(err,data){
- // we are now inside the called-back function
- if (err) {
- console.log('error reading myfile.txt');
- return;
- }
- if (data) {
- console.log('myfile.txt contains data and had no error');
- return;
- }
- });
- console.log('main: completed');
- // main will complete before readFile completes.
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement