JacobZwang
JacobZwang4mo ago

node stream.Readable.on not a function.

In Node we can listen to data on a readable stream using .on("data"). This method does not seem to exist in Deno. What should we use as a replacement and is it compatible with node?
1 Reply
Deno AI Helper
Deno AI Helper4mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, I can provide some guidance based on the information you've provided. In Deno, the concept of streams is handled a bit differently than in Node.js. Deno does not use the Node.js streams API, so methods like .on("data") are not available. Instead, Deno uses the standard WHATWG Streams API. For a readable stream in Deno, you would typically use an async iterator to consume the data. Here's a basic example of how you might read from a stream in Deno:
const file = await Deno.open('example.txt');
for await (const chunk of Deno.iter(file)) {
console.log(new TextDecoder().decode(chunk));
}
file.close();
const file = await Deno.open('example.txt');
for await (const chunk of Deno.iter(file)) {
console.log(new TextDecoder().decode(chunk));
}
file.close();
This code opens a file and reads it chunk by chunk, decoding each chunk as a string and logging it to the console. Note that this is not directly compatible with Node's .on("data") method, as Deno does not use the EventEmitter pattern that Node does. Please note that this advice is not based on the Deno documentation, and I would advise you to double-check the accuracy of this advice and refer to the latest Deno documentation or resources for the most accurate and up-to-date information.