Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A: yes it is possible for node to read 4 files at the same time. </p> <p>My answer would be, it depends on your situation, for reading the files synchronously or asynchronously. If it's configuration data, or the files can be cached, I would suggest just doing it synchronously, it's easy, and it's only done once. So you won't be waiting around very much. Long operations on initialization are typical, and can make things in the long run more efficient. That being said, reading four files in order, asynchronously, so that your program can do other things in the background isn't that hard. I will work on sync and async examples of each and add an edit.</p> <pre><code>/* jshint node:true*/ var fs = require('fs'); function readFilesSync(fileNames) { 'use strict'; var results = ''; for (var i = 0; i &lt; fileNames.length; i++) { results += fs.readFileSync(fileNames[i]); } return results; } function readFiles(fileNames, callback) { 'use strict'; var results = ''; function readFile(index) { if (index &lt; fileNames.length) { fs.readFile(fileNames[index], function (err, data) { results += data; readFile(index + 1); }); } else { callback(results); } } readFile(0); } function readAllFilesAtOnce(fileNames, callback) { 'use strict'; var results = {}; var numFiles = fileNames.length; function callBackWrapper() { var resultsOrdered = ''; for (var i = 0; i &lt; fileNames.length; i++) { resultsOrdered += results[fileNames[i]]; } callback(resultsOrdered); } function readFileAsync(fileName) { fs.readFile(fileName, function (err, data) { results[fileName] = data; numFiles--; if (numFiles === 0) { callBackWrapper(); } }); } for (var i = 0; i &lt; fileNames.length; i++) { readFileAsync(fileNames[i]); } } function doSomethingWithTheData(data) { 'use strict'; console.log('Results async: ' + data); } function doSomethingWithTheData2(data) { 'use strict'; console.log('Results async all at once: ' + data); } var fileNamesArray = ['blah.js', 'file.js', 'hello.txt']; console.log('The results sync: ' + readFilesSync(fileNamesArray)); readFiles(fileNamesArray, doSomethingWithTheData); readAllFilesAtOnce(fileNamesArray, doSomethingWithTheData2); </code></pre> <p>EDIT: There I added a method to read all of the files at once.</p> <p>Process.nextTick does no more than process this function on the next time around the event loop. EX:</p> <pre><code>process.nextTick(function() { console.log('never printed out'); }); while(true); </code></pre> <p>ex 2:</p> <pre><code>process.nextTick(function() { console.log('printed last'); }); console.log('printed first'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload