...

/

Solution: Asynchronous Streaming Adapter

Solution: Asynchronous Streaming Adapter

Convert a callback-based file reader into an async iterator using the [Symbol.asyncIterator]() protocol.

We'll cover the following...

Solution explanation

  • Lines 4–15: We define a legacyReader that reads data from a real file using Node’s fs module.

    • It opens a readable stream with fs.createReadStream(filename, { encoding: 'utf-8' }).

    • On each 'data' event, it passes the received chunk to the callback.

    • When the stream ends, it signals completion by calling the callback with null.

    • On errors, it logs the issue and also signals completion.

    • This mirrors how many legacy streaming APIs work—callback-driven and event-based.

  • Lines 17–40: The StreamAdapter converts that push model into a pull-based async iterator. This design lets the adapter synchronize two mismatched timing models:

    • The constructor initializes three critical internal properties:

      • queue: a buffer that temporarily stores chunks that have already arrived but haven’t yet been requested by the consumer.

      • resolveNext: a reference to a promise resolver function. This is used when the consumer has called next() but no new data has arrived yet—essentially “parking” the ...