-
Is there an equivalent to Node's import {Readable} from 'stream';
function* ids() {
for (const i = 0; i < 10; i++) {
yield i;
}
}
Readable.from(ids()); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Deno has WHATWG streams built in: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream. But this might be overkill, and so there is also the |
Beta Was this translation helpful? Give feedback.
-
Relevant issue and draft for supporting My use-case was to create a readable stream from an async-iterable so it can be returned as the body of an HTTP response using https://github.com/oakserver/oak. Is there an alternative/more idiomatic approach in Deno? |
Beta Was this translation helpful? Give feedback.
-
Perhaps this std import/function async function* ids() {
for (let i = 0; i < 10; i++) {
yield i;
}
}
// Create a stream
import {readableStreamFromAsyncIterator} from "https://deno.land/[email protected]/io/streams.ts";
const idStream = readableStreamFromAsyncIterator(ids());
// Consume the stream
import {collect} from "https://uber.danopia.net/deno/observables-with-streams@v1/sinks/collect.ts";
console.log('All IDs:', await collect(idStream)); As long as you're talking about async iterables, that is. If your iterable isn't async then std doesn't have an implementation. The non-async implementation is very basic though: from-iterable.ts |
Beta Was this translation helpful? Give feedback.
Perhaps this std import/function
readableStreamFromAsyncIterator
answers your original question: https://deno.land/[email protected]/io/streams.ts#L50As long as you're talking about async iterables, that is. If your iterable isn't async then std doesn't have an implementation. The non…