/** * @file Shims for the `Promise` statics newer than Node >= 18. * `Promise.withResolvers` landed in Node 22, `Promise.try` in Node 23. * Neither algorithm is the risky part; the details a hand-rolled version * skips are. `try` has to catch a SYNCHRONOUS throw from the callback and * reject with it rather than letting it escape, and it has to forward extra * arguments, which the obvious `new Promise(r => r(fn()))` spelling drops. * Both statics are `this`-generic: called on a `Promise` subclass they build * an instance of that subclass, and called on something that is not a * constructor they throw. Each one therefore has two exports: a `*SpecShim` * that reads its constructor from `this`, which is what a global install * uses, and the plain shim, which is the ergonomic free function this package * calls internally and which always builds a real `Promise`. */ export interface PromiseWithResolvers { promise: Promise; resolve: (value: T | PromiseLike) => void; reject: (reason?: unknown | undefined) => void; } export type PromiseTryFn = (fn: (...args: A) => T | PromiseLike, ...args: A) => Promise>; export type WithResolversFn = () => PromiseWithResolvers; /** * The spec's NewPromiseCapability over an arbitrary constructor. * * A non-constructor throws a TypeError, which is what makes both statics * `this`-generic rather than hardwired to `Promise`. The executor runs * synchronously during construction, so both functions are assigned before this * returns; a constructor that fails to call it leaves them undefined, and the * spec throws for that too. */ export declare function newPromiseCapability(ctor: unknown): PromiseWithResolvers; /** * The native `Promise.try`, or undefined below Node 23. */ export declare const promiseTryNative: PromiseTryFn | undefined; /** * `Promise.try` shim, always building a real `Promise`. */ export declare function promiseTryShim(fn: (...args: A) => T | PromiseLike, ...args: A): Promise>; /** * `Promise.try` in its spec shape: the constructor comes from `this`. * * The call sits INSIDE the try so a synchronous throw becomes a rejection, * which is the whole reason the method exists. */ export declare function promiseTrySpecShim(this: unknown, fn: (...args: A) => T | PromiseLike, ...args: A): Promise>; export declare const promiseTry: PromiseTryFn; /** * The native `Promise.withResolvers`, or undefined below Node 22. */ export declare const promiseWithResolversNative: WithResolversFn | undefined; /** * `Promise.withResolvers` shim, always building a real `Promise`. */ export declare function promiseWithResolversShim(): PromiseWithResolvers; /** * `Promise.withResolvers` in its spec shape: the constructor comes from `this`. */ export declare function promiseWithResolversSpecShim(this: unknown): PromiseWithResolvers; export declare const promiseWithResolvers: WithResolversFn;