coder
coder

Reputation: 75

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());
      });
    });
  }***

Upvotes: 1

Views: 1570

Answers (2)

Dmitriy Mozgovoy
Dmitriy Mozgovoy

Reputation: 1597

const fs= require('fs').promises;

async function readFiles(files) {
    const results = await Promise.allSettled(files.map(file=> fs.readFile(file)));
    console.log(results);
    return results;
}

readFiles(['1.txt', '2.txt', '3.txt']);

Upvotes: 1

Terry Lennox
Terry Lennox

Reputation: 30725

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']);

Upvotes: 2

Related Questions