From ee1b8dc883db1531d913f7b10a8d1160f3209d93 Mon Sep 17 00:00:00 2001 From: Vincent LE GOFF Date: Tue, 12 Nov 2019 21:51:14 +0100 Subject: feat: std/node (#3319) --- std/node/_utils.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 std/node/_utils.ts (limited to 'std/node/_utils.ts') diff --git a/std/node/_utils.ts b/std/node/_utils.ts new file mode 100644 index 000000000..f0808e82b --- /dev/null +++ b/std/node/_utils.ts @@ -0,0 +1,35 @@ +export function notImplemented(msg?: string): never { + const message = msg ? `Not implemented: ${msg}` : "Not implemented"; + throw new Error(message); +} + +// API helpers + +export type MaybeNull = T | null; +export type MaybeDefined = T | undefined; +export type MaybeEmpty = T | null | undefined; + +export function intoCallbackAPI( + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + func: (...args: any[]) => Promise, + cb: MaybeEmpty<(err: MaybeNull, value: MaybeEmpty) => void>, + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + ...args: any[] +): void { + func(...args) + .then(value => cb && cb(null, value)) + .catch(err => cb && cb(err, null)); +} + +export function intoCallbackAPIWithIntercept( + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + func: (...args: any[]) => Promise, + interceptor: (v: T1) => T2, + cb: MaybeEmpty<(err: MaybeNull, value: MaybeEmpty) => void>, + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + ...args: any[] +): void { + func(...args) + .then(value => cb && cb(null, interceptor(value))) + .catch(err => cb && cb(err, null)); +} -- cgit v1.2.3