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

197
Views
Get first element of an async iterable

On nodejs, I’m trying to get the first line of a file. For that, I’m using the readline package as mentioned in this answer.

But since I’m only trying to get the first line, I’d need to get the readline object to output its first element. I found how to do that in this answer, but it looks like it only works for synchronous iterators.

How do you get the first element of an async iterator?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just return in the first iteration of the loop? Copying some code from that answer, and making sure to destroy the stream after you're done with getting only the first line:

async function processLineByLine() {
  const fileStream = fs.createReadStream('input.txt');
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity
  });
  for await (const line of rl) {
    fileStream.destroy();
    return line;
  }
}

and then processLineByLine will return a Promise that resolves to the content in the first line.

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!