/** * Interface for the return type of promiseWithResolvers */ export interface PromiseWithResolvers { promise: Promise; resolve: (value: T | PromiseLike) => void; reject: (reason?: any) => void; } /** * Node 20+ compatible implementation of Promise.withResolvers() * Falls back to native Promise.withResolvers when available (Node 22+) */ export function promiseWithResolvers(): PromiseWithResolvers { // Use native implementation if available if (typeof Promise.withResolvers === "function") { return Promise.withResolvers(); } // Fallback implementation for Node 20+ let resolve!: (value: T | PromiseLike) => void; let reject!: (reason?: any) => void; const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); return { promise, resolve, reject }; }