more TS 4.1 fixes (#106358)

This commit is contained in:
Benjamin Pasero 2020-09-10 11:28:59 +02:00
parent 41794e5631
commit 72f468f569

View file

@ -52,21 +52,21 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined>; export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken): Promise<T | undefined>;
export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue: T): Promise<T>; export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue: T): Promise<T>;
export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue?: T): Promise<T> { export function raceCancellation<T>(promise: Promise<T>, token: CancellationToken, defaultValue?: T): Promise<T | undefined> {
return Promise.race([promise, new Promise<T>(resolve => token.onCancellationRequested(() => resolve(defaultValue)))]); return Promise.race([promise, new Promise<T | undefined>(resolve => token.onCancellationRequested(() => resolve(defaultValue)))]);
} }
export function raceTimeout<T>(promise: Promise<T>, timeout: number, onTimeout?: () => void): Promise<T> { export function raceTimeout<T>(promise: Promise<T>, timeout: number, onTimeout?: () => void): Promise<T | undefined> {
let promiseResolve: (() => void) | undefined = undefined; let promiseResolve: ((value: T | undefined) => void) | undefined = undefined;
const timer = setTimeout(() => { const timer = setTimeout(() => {
promiseResolve?.(); promiseResolve?.(undefined);
onTimeout?.(); onTimeout?.();
}, timeout); }, timeout);
return Promise.race([ return Promise.race([
promise.finally(() => clearTimeout(timer)), promise.finally(() => clearTimeout(timer)),
new Promise<T>(resolve => promiseResolve = resolve) new Promise<T | undefined>(resolve => promiseResolve = resolve)
]); ]);
} }