8000 Introducing promisify utility function. · evalcode/web-build-tools@b086891 · GitHub
[go: up one dir, main page]

Skip to content

Commit b086891

Browse files
committed
Introducing promisify utility function.
1 parent e0d4b51 commit b086891

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

common/reviews/api/node-core-library.api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ enum PosixModeBits {
298298
UserWrite = 128
299299
}
300300

301+
// @beta
302+
class PromiseUtilities {
303+
static promiseify<TResult, TError>(fn: (cb: callback<TResult, TError>) => void): Promise<TResult>;
304+
}
305+
301306
// @public
302307
class ProtectableMap<K, V> {
303308
constructor(parameters: IProtectableMapParameters<K, V>);
@@ -323,3 +328,4 @@ class Text {
323328

324329
// WARNING: Unsupported export: ExecutableStdioStreamMapping
325330
// WARNING: Unsupported export: ExecutableStdioMapping
331+
// WARNING: Unsupported export: callback
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
export type callback<TResult, TError> = (error: TError, result: TResult) => void;
5+
6+
/**
7+
* This is a set of utilities for constructing and interacting with promises.
8+
*
9+
* @beta
10+
*/
11+
export class PromiseUtilities {
12+
/**
13+
* This function wraps a function with a callback in a promise.
14+
*/
15+
public static promiseify<TResult, TError>(
16+
fn: (cb: callback<TResult, TError>) => void
17+
): Promise<TResult>;
18+
public static promiseify<TResult, TError, TArg1>(
19+
fn: (arg1: TArg1, cb: callback<TResult, TError>) => void,
20+
arg1: TArg1
21+
): Promise<TResult>;
22+
public static promiseify<TResult, TError, TArg1, TArg2>(
23+
fn: (arg1: TArg1, arg2: TArg2, cb: callback<TResult, TError>) => void,
24+
arg1: TArg1,
25+
arg2: TArg2
26+
): Promise<TResult>;
27+
public static promiseify<TResult, TError, TArg1, TArg2>(
28+
fn: (
29+
a: TArg1 | callback<TResult, TError>,
30+
b?: TArg2 | callback<TResult, TError>,
31+
c?: TArg2 | callback<TResult, TError>
32+
) => void,
33+
arg1?: TArg1,
34+
arg2?: TArg2
35+
): Promise<TResult> {
36+
return new Promise((resolve: (result: TResult) => void, reject: (error: TError) => void) => {
37+
const cb: callback<TResult, TError> = (error: TError, result: TResult) => {
38+
if (error) {
39+
reject(error);
40+
} else {
41+
resolve(result);
42+
}
43+
};
44+
45+
try {
46+
if (arg1 && arg2) {
47+
fn(arg1, arg2, cb);
48+
} else if (arg1) {
49+
fn(arg1, cb);
50+
} else {
51+
fn(cb);
52+
}
53+
} catch (e) {
54+
reject(e);
55+
}
56+
});
57+
}
58+
}

libraries/node-core-library/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ export {
6969
FileWriter,
7070
IFileWriterFlags
7171
} from './FileWriter';
72+
export {
73+
PromiseUtilities,
74+
callback
75+
} from './PromiseUtilities';

0 commit comments

Comments
 (0)
0