diff options
Diffstat (limited to 'js/util.ts')
-rw-r--r-- | js/util.ts | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/js/util.ts b/js/util.ts index d4563936f..c1e84610f 100644 --- a/js/util.ts +++ b/js/util.ts @@ -49,15 +49,20 @@ export function arrayToStr(ui8: Uint8Array): string { // produces broken code when targeting ES5 code. // See https://github.com/Microsoft/TypeScript/issues/15202 // At the time of writing, the github issue is closed but the problem remains. -export interface Resolvable<T> extends Promise<T> { +export interface ResolvableMethods<T> { resolve: (value?: T | PromiseLike<T>) => void; // tslint:disable-next-line:no-any reject: (reason?: any) => void; } + +type Resolvable<T> = Promise<T> & ResolvableMethods<T>; + export function createResolvable<T>(): Resolvable<T> { - let methods; + let methods: ResolvableMethods<T>; const promise = new Promise<T>((resolve, reject) => { methods = { resolve, reject }; }); - return Object.assign(promise, methods) as Resolvable<T>; + // TypeScript doesn't know that the Promise callback occurs synchronously + // therefore use of not null assertion (`!`) + return Object.assign(promise, methods!) as Resolvable<T>; } |