Skip to content

Latest commit

 

History

History
22 lines (15 loc) · 764 Bytes

every-readable-stream-is-asynchronously-iterable.md

File metadata and controls

22 lines (15 loc) · 764 Bytes

Every Readable Stream is Asynchronously Iterable

Every readable stream is asynchronously iterable, which means that we can use a for await...of loop to read the stream's contents:

import * as fs from "fs";

async function logChunks(readable) {
  for await (const chunk of readable) {
    consoel.log(chunk);
  }
}

const readable = fs.createReadStream("tmp/test.txt", { encoding: "utf8" });

logChunks(readable);
// 'This is a test!\n'

Shout out to Marshall for the pointer on this one ❤️