Asynchronous File Reading

The asynchronous method of file reading puts the read request on the event queue and then returns control back to the calling code. The actual read does not take place until the event loop picks up the read request and executes it. You need to be careful when performing multiple asynchronous read requests on the same file because you cannot guarantee their execution order unless you wait for the first read callback to execute before executing the next read. Typically the simplest way to do this is to nest reads inside the callback from the previous read. The code in Listing 6.7 illustrates this process.

To read from a file asynchronously, first open it by using open() and then after the callback from the open request has executed, use read() to read data from the file. The following is the syntax for read():

fs.read(fd, buffer, offset, length, position, callback)

The fd parameter is the file descriptor that openSync() returns. The buffer parameter specifies the Buffer object that data will be read into from the file. The offset parameter specifies the index in the buffer to begin reading data; if you want to begin at the current index in the buffer, this value should be null. The length parameter specifies the number of bytes to read; to read until the end of the buffer, specify null. The position argument specifies the position in the file to begin reading; specify null for this value to use the current file position.

The callback argument must be a function that can accept three parameters: error, bytes, and buffer. The error parameter is an error if one occurred during the read, bytes specifies the number of bytes read, and buffer is the buffer with data populated from the read request.

The code in Listing 6.7 shows how to implement basic asynchronous reading to read chunks of data from a file. Notice that the callback specified in lines 16–18 in the open() callback calls the readFruit() function and passes the file descriptor. Also notice that the read() callback specified in lines 5–13 also calls readFruit() and passes the file descriptor. This ensures that the asynchronous read completes before another executes. Figure 6.7 shows the output of the code in Listing 6.7.

Listing 6.7 file_read_async.js: Performing asynchronous reads from a file


01 var fs = require('fs'),
02 function readFruit(fd, fruits){
03   var buf = new Buffer(5);
04   buf.fill();
05   fs.read(fd, buf, 0, 5, null, function(err, bytes, data){
06       if ( bytes > 0) {
07         console.log("read %dbytes", bytes);
08         fruits += data;
09         readFruit(fd, fruits);
10       } else {
11         fs.close(fd);
12         console.log ("Fruits: %s", fruits);
13       }
14   });
15 }
16 fs.open('../data/fruit.txt', 'r', function(err, fd){
17   readFruit(fd, "");
18 });


Image

Figure 6.7 Output of file_read_async.js, reading asynchronously from a file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.150.75