Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

875 B
Raw Blame
export class PromiseScheduler {
    public readonly promises: Set<Promise<any>> = new Set()

    public schedule<T>(promise: Promise<T>): Promise<T>
    public schedule<T extends readonly [Promise<unknown>, Promise<unknown>, ...Promise<unknown>[]]>(
        ...promises: T
    ): Promise<{ -readonly [K in keyof T]: Awaited<T[K]> }>
    public schedule(...promises: Promise<unknown>[]): Promise<unknown> {
        for (const promise of promises) {
            this.promises.add(promise)
            const cleanup = () => this.promises.delete(promise)
            promise.then(cleanup, cleanup)
        }
        return promises.length === 1 ? promises[0] : Promise.all(promises)
    }

    public async waitForAll() {
        return await Promise.all(this.promises)
    }

    public async waitForAllSettled() {
        return await Promise.allSettled(this.promises)
    }
}