mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 12:19:12 +00:00
feat: std/node (#3319)
This commit is contained in:
parent
0f33bf6885
commit
ee1b8dc883
7 changed files with 327 additions and 0 deletions
35
std/node/_utils.ts
Normal file
35
std/node/_utils.ts
Normal file
|
@ -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> = T | null;
|
||||
export type MaybeDefined<T> = T | undefined;
|
||||
export type MaybeEmpty<T> = T | null | undefined;
|
||||
|
||||
export function intoCallbackAPI<T>(
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
func: (...args: any[]) => Promise<T>,
|
||||
cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T>) => 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<T1, T2>(
|
||||
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
|
||||
func: (...args: any[]) => Promise<T1>,
|
||||
interceptor: (v: T1) => T2,
|
||||
cb: MaybeEmpty<(err: MaybeNull<Error>, value: MaybeEmpty<T2>) => 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));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue