79460287

Date: 2025-02-22 20:51:37
Score: 0.5
Natty:
Report link

I wrote a module read-next-line, which provide functionality to iterate over each line of text read via a Web API ReadableStream.

The following code snippets shows how that is done, which streams from an online text file which is streamed using fetch:

// import {ReadNextLine} from 'read-next-line';

async function run() {
  const {ReadNextLine} = await import('https://cdn.jsdelivr.net/npm/[email protected]/+esm');
 
  const response = await fetch('https://raw.githubusercontent.com/Borewit/read-next-line/refs/heads/master/test/samples/lorem-ipsem.utf-8.txt');
  
  if(!response.ok) {
    console.error(`HTTP-response-status=${response.status}`);
    return;
  }
  console.log('Parsing text stream...');

  const reader = new ReadNextLine(response.body);

  let line;
  let lineNr = 0;
  while (lineNr < 10 && (line = await reader.readLine()) !== null) {
    console.log(`Line #${++lineNr}: ${line}`); // Process each line as needed
  }
}

run().catch(e => console.error(e));

Reasons:
  • Contains signature (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Borewit