/** * A promise with exposed resolve and reject methods. This allows you to create a promise without any parameters * and then call resolve or reject on it at any time. This enables you to hand out promises to callers without having * initiated or decided on how and when to resolve the promise with minimal overhead. Just call resolve on it and * all waiting parties will have their then function callbaks invoked. * * The weird constructor is due to the special requirements and * expectations on the inner workings of a promise. * https://www.ecma-international.org/ecma-262/7.0/index.html#sec-newpromisecapability * https://stackoverflow.com/questions/48158730/extend-javascript-promise-and-resolve-or-reject-it-inside-constructor/48159603 */ export class OpenPromise extends Promise { public resolve: ((value: T) => void); public reject: ((reason: any) => void); constructor(executor: (resolve: (value: T | PromiseLike) => void, reject: (reason: any) => void) => void = (resolve: (value: T | PromiseLike) => void, reject: (reason: any) => void) => { }) { let res, rej; super((resolve, reject) => { executor(resolve, reject); res = resolve; rej = reject; }); this.resolve = res as unknown as (value?: T | PromiseLike | undefined) => void; this.reject = rej as unknown as (reason?: any) => void; } }