B
B
Bogdan2019-11-25 02:44:04
Node.js
Bogdan, 2019-11-25 02:44:04

Asynchronous ReadStream?

Hello. Tell me please. What is the best way to do it, there is a file that needs to be read 100 lines, sent to the database, get a response from the database and read the next 100 lines?
I kind of googled the option, but maybe there is a better one? Thank you.

const fs = require('fs');
const readline = require('readline');

(async() => {
  const delay = t => new Promise(r => setTimeout(r, t));
  const fileStream = fs.createReadStream('./tmp/1.txt');
  fileStream.on('end', async () => console.log('end'));

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });

  for await (const line of rl) {
    await delay(50);
    console.log(`Line from file: ${line}`);
  }
})();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question