node: Add ability to promisify setTimeout and setImmediate (#19488)

* node: Add ability to promisify setTimeout and setImmediate

* Support providing a value
This commit is contained in:
Andy
2017-09-08 12:51:56 -07:00
committed by GitHub
parent 2bb7adf1ce
commit f344702d8e
2 changed files with 25 additions and 0 deletions

16
types/node/index.d.ts vendored
View File

@@ -81,10 +81,18 @@ declare var __filename: string;
declare var __dirname: string;
declare function setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
declare namespace setTimeout {
export function __promisify__(ms: number): Promise<void>;
export function __promisify__<T>(ms: number, value: T): Promise<T>;
}
declare function clearTimeout(timeoutId: NodeJS.Timer): void;
declare function setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
declare function clearInterval(intervalId: NodeJS.Timer): void;
declare function setImmediate(callback: (...args: any[]) => void, ...args: any[]): any;
declare namespace setImmediate {
export function __promisify__(): Promise<void>;
export function __promisify__<T>(value: T): Promise<T>;
}
declare function clearImmediate(immediateId: any): void;
// TODO: change to `type NodeRequireFunction = (id: string) => any;` in next mayor version.
@@ -5666,10 +5674,18 @@ declare module "v8" {
declare module "timers" {
export function setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
export namespace setTimeout {
export function __promisify__(ms: number): Promise<void>;
export function __promisify__<T>(ms: number, value: T): Promise<T>;
}
export function clearTimeout(timeoutId: NodeJS.Timer): void;
export function setInterval(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;
export function clearInterval(intervalId: NodeJS.Timer): void;
export function setImmediate(callback: (...args: any[]) => void, ...args: any[]): any;
export namespace setImmediate {
export function __promisify__(): Promise<void>;
export function __promisify__<T>(value: T): Promise<T>;
}
export function clearImmediate(immediateId: any): void;
}

View File

@@ -2177,6 +2177,15 @@ namespace timers_tests {
timeout.ref();
timers.clearTimeout(timeout);
}
async function testPromisify() {
const setTimeout = util.promisify(timers.setTimeout);
let v: void = await setTimeout(100); // tslint:disable-line no-void-expression void-return
let s: string = await setTimeout(100, "");
const setImmediate = util.promisify(timers.setImmediate);
v = await setImmediate(); // tslint:disable-line no-void-expression
s = await setImmediate("");
}
}
/////////////////////////////////////////////////////////