/** * A `Promise.withResolvers` implementation that exposes the `resolve` and * `reject` functions on a `Promise`. * * @see https://tc39.es/proposal-promise-with-resolvers/ */ export class DetachedPromise { public readonly resolve: (value: T | PromiseLike) => void public readonly reject: (reason: any) => void public readonly promise: Promise constructor() { let resolve: (value: T | PromiseLike) => void let reject: (reason: any) => void // Create the promise and assign the resolvers to the object. this.promise = new Promise((res, rej) => { resolve = res reject = rej }) // We know that resolvers is defined because the Promise constructor runs // synchronously. this.resolve = resolve! this.reject = reject! } }