0

I would to write a simple NodeJS app, that reads a file, analyzes its lines one-by-one and writes the result. Here is the code sample that works

var lines;
var fs = require('fs');
var data="";
fs.readFile('list.csv', 'ascii', function(err,data){
    if(err) {
        console.error("Could not open file: %s", err);
        process.exit(1);
    }
    var data2=data.split(/[\n\r]+/);
    for(var i=0; i<data2.length; i++){
        /*LISTING ALL THE LIST LINE-BY-LINE */
        console.log(i + data2[i]);
    }
});

I'd like to know why I should write my code inside function(err,data){..*my-code*..}? I tried to declare all the variables as global and write

console.log();

at the end of the code – it seems it just dosen't execute this code line. So, why cant I write my code outside function(err,data){}?

Chris
  • 44,602
  • 16
  • 137
  • 156
f1nn
  • 6,989
  • 24
  • 69
  • 92
  • 1
    Ilya, see this answer: http://stackoverflow.com/questions/9362823/why-is-a-function-and-a-callback-non-blocking-in-node-js/9363071#9363071 – Linus Thiel Mar 27 '12 at 08:48

3 Answers3

1

One of the main features of NodeJS is the non-blocking event loop. When functions take a long time, the Node script doesn't wait for the function to return, which is why you need to pass in your code in a callback.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99
  • So, if I'd like to write a non-blocking script, I should process callback writing my code inside the callback function? – f1nn Mar 27 '12 at 09:20
  • Yes, just like in the demo you posted. If you don't, there is no guarantee whether your code will run before or after the `readFile` function - so you may or may not get the data. – Adam Hopkinson Mar 27 '12 at 11:14
0

If you should need a "blocking behavior" you can use the synchronous fs methods.

Please see the documentation:

http://nodejs.org/api/fs.html#fs_fs_readfilesync_filename_encoding

Julian Hollmann
  • 2,902
  • 2
  • 25
  • 44
0

By default all file system operations are non-blocking, so the result needs to be used in a callback function that runs when the operation completes, in this case when the file has finished being read into a variable.

While it's generally a better design to use non-blocking IO to allow the server to run multiple operations at the same time, you can switch most of the fs operations to run synchronously:

var data = fs.readFileSync('list.csv', 'ascii');
steveukx
  • 4,370
  • 19
  • 27
  • That worked perfectly, thanks. Although, I've nothiced that this code works a bit slower than an asynchronous one I've listed before. – f1nn Mar 27 '12 at 09:21