Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.2 kB ยท 71 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172export async function* yieldMany<T>(promises: Promise<T>[]) { // Race-free settle-as-they-arrive: each promise pushes into a queue when it // resolves; the consumer drains the queue and parks on a fresh wake-up // promise when empty. This avoids the splice/Promise.race pattern, which // can drop values when multiple promises settle in the same microtask // batch (both spliced before the next race iteration). Rejections are // surfaced as thrown values to the caller โ the underlying probe paths // are expected to convert failures into result objects upstream. const queue: T[] = []; let pending = promises.length; let wake: (() => void) | null = null; let rejection: { error: unknown } | null = null;
for (const p of promises) { p.then( (value) => { queue.push(value); pending--; wake?.(); }, (error) => { // First rejection wins. Subsequent rejections in the same microtask // batch must not silently overwrite โ the consumer throws on the // first one and any later ones would be lost. Probes are expected // to convert per-region failures into result objects upstream, so // landing here at all is exceptional. if (!rejection) rejection = { error }; pending--; wake?.(); }, ); }
while (pending > 0 || queue.length > 0) { if (queue.length === 0 && rejection === null) { await new Promise<void>((resolve) => { wake = resolve; }); wake = null; } while (queue.length > 0) { // oxlint-disable-next-line typescript/no-non-null-assertion -- length-guarded above yield queue.shift()!; } if (rejection) { const { error } = rejection; rejection = null; throw error; } }
return "done";}
export function iteratorToStream(iterator: AsyncGenerator) { return new ReadableStream({ async pull(controller) { try { const { value, done } = await iterator.next(); if (done) { controller.close(); } else { controller.enqueue(value); } } catch (err) { console.error("Stream error:", err); controller.error(err); } }, });}