// @loaders.gl, MIT license // type Parameter1 unknown> = T extends ( // arg1: infer P, // ...args: unknown[] // ) => unknown // ? P // : never; // type Parameter2 unknown> = T extends ( // arg1: unknown, // arg2: infer P, // ...args: unknown[] // ) => unknown // ? P // : never; // type CallbackParameter2< // T extends (arg: unknown, cb: (error: any, value: any) => void) => unknown // > = T extends (arg: unknown, cb: (error: any, value: infer P) => void) => unknown ? P : never; // type CallbackParameter3< // T extends (arg: unknown, arg2: unknown, cb: (error: any, value: any) => void) => unknown // > = T extends (arg: unknown, arg2: unknown, cb: (error: any, value: infer P) => void) => unknown // ? P // : never; // /** Extract the parameters of a function type in a tuple */ // export type Promisified void) => any> = ( // arg1: Parameter1 // ) => Promise>; // /** Extract the parameters of a function type in a tuple */ // export type Promisified2 void) => any> = ( // arg1: Parameter1, // arg2: Parameter2 // ) => Promise>; /** Wrapper for Node.js promisify */ type Callback = (error: unknown, args: A) => void; /** * Typesafe promisify implementation * @link https://dev.to/_gdelgado/implement-a-type-safe-version-of-node-s-promisify-in-7-lines-of-code-in-typescript-2j34 * @param fn * @returns */ export function promisify1(fn: (args: T, cb: Callback) => void): (args: T) => Promise { return (args: T) => new Promise((resolve, reject) => fn(args, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))) ); } export function promisify2( fn: (arg1: T1, arg2: T2, cb: Callback) => void ): (arg1: T1, arg2: T2) => Promise { return (arg1: T1, arg2: T2) => new Promise((resolve, reject) => fn(arg1, arg2, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))) ); } export function promisify3( fn: (arg1: T1, arg2: T2, arg3: T3, cb: Callback) => void ): (arg1: T1, arg2: T2, arg3: T3) => Promise { return (arg1: T1, arg2: T2, arg3: T3) => new Promise((resolve, reject) => fn(arg1, arg2, arg3, (error, callbackArgs) => (error ? reject(error) : resolve(callbackArgs))) ); }