...

/

Asynchronous Streaming Adapter

Asynchronous Streaming Adapter

Adapt a callback-based file reader into an async iterator interface that works with for await...of.

We'll cover the following...

Problem statement

You maintain a legacy file reader utility that processes data in chunks. It uses a callback pattern to push each chunk of data, as shown:

legacyReader.readFile('log.txt', chunk => { ... });

Modern code in your app expects an async iterator that it can loop over using:

for await (const chunk of reader) { ... }

You need to write an adapter that makes the legacy callback-based reader compatible with this newer iteration model—without modifying the original code.

Goal

Implement a StreamAdapter class that wraps a legacy reader and exposes an async iterable interface.

for await (const chunk of new StreamAdapter(legacyReader, 'file.txt')) { ... }
...