Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda