Search code examples
node.jsfsreadfileasync.js

Read files simultaneously using nodejs fs


I wanted to know what is the most efficient method for reading multiple files at the same time. i have an array of files paths and I want to create an array of data using fs read file (not asyncRead and not using call back function) arranged according to the index of the array of paths. i need to read three files simultaneously, And I can not find how to simultaneously read files.

I found this method but can't figure out if it reads the files at the same time and if so how:

***function simultaneouslyRead(filePath1, filePath2){
    var async = require('async'),
    fs = require('fs'),
    files = [filePath1, filePath2];
    async.map(files, fs.readFile, function(err, files) {
        if(err) {
            throw err;
        }
        files.forEach(function(file) {
          console.log(file.toString());
      });
    });
  }***

Solution

  • We could use util.promisify to create a promise based version of fs.readFile, then use Promise.all to read all the files into an array, something like this:

    const fs = require("fs");
    const { promisify } = require("util");
    
    const promiseReadFile = promisify(fs.readFile);
    
    async function readFiles(files) {
        const fileArray = await Promise.all(files.map(file => promiseReadFile(file)));
        console.log("File contents:", fileArray);
        return fileArray;
    }
    
    readFiles(['file1.txt', 'file2.txt']);
    

    The fileArray will contain an array of Buffers once the data has been read.

    A revised version, incorporating errors from any read failures:

    const fs = require("fs");
    
    function promiseReadFile(path, options = {}) {
        return new Promise(resolve => { 
            fs.readFile(path, options, (err, data) => resolve({ err, data }));
        });
    }
    
    async function readFiles(files) {
        const fileArray = await Promise.all(files.map(file => promiseReadFile(file)));
        console.log("File contents:", fileArray);
        return fileArray;
    }
    
    readFiles(['file1.txt', 'file2.txt', 'file3.txt']);