Asynchronous reading and caching of multiple files in nodejs

I have an array that contains the URL of several files. For instance:

var files = ['1.html', '2.html', '3.html']; 

I need to read them asynchronously and save them in an object called cache (cache = {}). For this, I used the code:

 for(var i = 0; i < files.length; i++){ require('fs').readFile(files[i], 'utf8', function (error,data) { cache[files[i]]=data; }); } 

As a result, I have the result:

 cache = { undefined : 'File 3 content' } 

I understand that "readFile" is valid after the loop is completed and it loses it. Is there a way to fix this or another method for reading files from an array and caching them?

+4
source share
2 answers

When your readFile response is executed, the for loop will already be completed. Thus, i will be files.length , and files[i] will be undefined . To mitigate this, you need to wrap the variables in closure. The easiest way to do this is to create a function that executes your readFile call, and call it in a loop:

 function read(file) { require('fs').readFile(file, 'utf8', function (error,data) { cache[file]=data; }); } for(var i = 0; i < files.length; i++){ read(files[i]); } 

For even better control over execution, you can look at async :

 function readAsync(file, callback) { fs.readFile(file, 'utf8', callback); } async.map(files, readAsync, function(err, results) { // results = ['file 1 content', 'file 2 content', ...] }); 

Edit: Use a helper function for the async example.

+16
source

The existing answer did not work for me. I found the NPM package that did the job: https://www.npmjs.com/package/read-multiple-files . After npm install read-multiple-files on the command line, here is the code I used:

 var files = ['1.html', '2.html', '3.html']; console.log("\n"); readMultipleFiles(files, 'utf8', function(err, inputFiles) { if(err) { console.log("Read Error: " + err); } fileOne = inputFiles[0]; fileTwo = inputFiles[1]; ... console.log(fileOne); console.log(fileTwo); }); 
0
source

Source: https://habr.com/ru/post/1400383/


All Articles