diff --git a/core/async.ts b/core/async.ts index a719184..300cd35 100644 --- a/core/async.ts +++ b/core/async.ts @@ -87,6 +87,15 @@ export class EventStream implements AsyncIterable { this.#done = true this.#resolve() } + + /** + * Iterate over the event stream, calling a callback for each event. + * @author MindfulMinun + * @since 2024-06-02 + */ + each(callback: (value: T) => void) { + return each(this, callback) + } } /** @@ -104,6 +113,17 @@ export async function once(source: AsyncIterable, predicate: (value: T) => } } +/** + * Iterate over an async iterable, calling a callback for each value. + * + * @author MindfulMinun + * @since 2024-06-02 + */ +export async function each(source: AsyncIterable, callback: (value: T) => void) { + for await (const value of source) { + callback(value) + } +} /** @@ -143,4 +163,3 @@ export function pinkyPromise(): [ // the assignment to `p` return [p, resolve!, reject!] } -