import { toAsyncIterable } from "https://deno.land/x/ayonli_jsext@v0.9.72/reader.ts";
Wraps a source as an AsyncIterable
object that can be used in the
for await...of...
loop for reading streaming data.
Examples
Example 1
Example 1
import { toAsyncIterable } from "@ayonli/jsext/reader";
const res = new Response("Hello, World!");
for await (const chunk of toAsyncIterable(res.body!)) {
console.log("receive chunk:", chunk);
}
Parameters
stream: ReadableStream<T> | Promise<ReadableStream<T>>
Examples
Example 1
Example 1
import { toAsyncIterable } from "@ayonli/jsext/reader";
// listen to the `onmessage`
const sse = new EventSource("/sse/message");
for await (const msg of toAsyncIterable(sse)) {
console.log("receive message:", msg);
}
// listen to a specific event
const channel = new EventSource("/sse/broadcast");
for await (const msg of toAsyncIterable(channel, { event: "broadcast" })) {
console.log("receive message:", msg);
}
Examples
Example 1
Example 1
import { toAsyncIterable } from "@ayonli/jsext/reader";
const ws = new WebSocket("/ws");
for await (const msg of toAsyncIterable(ws)) {
if (typeof data === "string") {
console.log("receive text message:", data);
} else {
console.log("receive binary data:", data);
}
}
Examples
Example 1
Example 1
import { toAsyncIterable } from "@ayonli/jsext/reader";
for await (const msg of toAsyncIterable(self)) {
console.log("receive message from the parent window:", msg);
}
Parameters
target: EventTarget
Examples
Example 1
Example 1
import { toAsyncIterable } from "@ayonli/jsext/reader";
for await (const msg of toAsyncIterable(process)) {
console.log("receive message from the parent process:", msg);
}