async.ts: Add each, EventStream#each

This commit is contained in:
mindfulminun committed 2024-06-02 23:24:58 -05:00
1 parent a8372433f5
commit 9cf0bf415b
1 file changed
+20 -1
+20 -1
View File
@@ -87,6 +87,15 @@ export class EventStream<T> implements AsyncIterable<T> {
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<T>(source: AsyncIterable<T>, predicate: (value: T) =>
}
}
/**
* Iterate over an async iterable, calling a callback for each value.
*
* @author MindfulMinun
* @since 2024-06-02
*/
export async function each<T>(source: AsyncIterable<T>, callback: (value: T) => void) {
for await (const value of source) {
callback(value)
}
}
/**
@@ -143,4 +163,3 @@ export function pinkyPromise<T>(): [
// the assignment to `p`
return [p, resolve!, reject!]
}