Skip to content

Instantly share code, notes, and snippets.

@gflarity
Created March 15, 2023 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gflarity/12e7b9774705e704d7c55799881e4f50 to your computer and use it in GitHub Desktop.
Save gflarity/12e7b9774705e704d7c55799881e4f50 to your computer and use it in GitHub Desktop.
Some Deno code for reading a file a line by line asychronously
// function that returns reads a file line by line asynchronously
export async function* readFileLineByLine(file: string) : AsyncGenerator<string> {
const textDecoder = new TextDecoder();
let stringBuffer: string = "";
const fileHandle = await Deno.open(file);
try {
let intBuffer = new Uint8Array(1000);
let numberOfBytesRead = await fileHandle.read(intBuffer);
while (numberOfBytesRead !== null) {
const asString = textDecoder.decode(intBuffer);
stringBuffer += asString;
while (stringBuffer.includes("\n")) {
const indexOfNewLine = stringBuffer.indexOf("\n");
const newLine = stringBuffer.slice(0, indexOfNewLine);
yield newLine;
// replace the stringBuffer with the remaining string and keep processing
stringBuffer = stringBuffer.slice(indexOfNewLine + 1);
}
intBuffer = new Uint8Array(1000);
numberOfBytesRead = await fileHandle.read(intBuffer);
}
} finally {
fileHandle.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment