Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
How to store data from reading lines in nodejs

Hello I have external txt file with data to my function. How can I store read line to variable

const fs = require("fs");
const readline = require("readline");
const firstVariable; [it is firstLine]
const secondVariable; [it is secondLine]
const thirdVariable; [it is thirdLine]
const fourthVariable; [it is fourthLine]

const readInterface = readline.createInterface({
  input: fs.createReadStream("./slo1.in"),
  output: process.stdout,
  terminal: false,
});

readInterface.on("line", function (line) {
  console.log(line);
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here is a better solution:

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

const readFile = () => {
return new Promise((resolve, reject) => {
    const lineArray = [];
    const readInterface = readline.createInterface({
        input: fs.createReadStream("./slo1.in"),
        output: process.stdout,
        terminal: false,
    });

    readInterface.on("line", (line) => {
        lineArray.push(line);
    }).on("close", () => {
        resolve(lineArray);
    });
})
}



readFile().then(res => {
    console.log(JSON.stringify(res))
})


// or you can use async/await syntax
async function readAsync() {
    const res = await readFile();
    console.log(res)
}

readAsync();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!