Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.19 KB

File metadata and controls

51 lines (37 loc) · 1.19 KB

typelab / utils / FunctionPromisify

type FunctionPromisify<T> = [ExtractParams<T>, ExtractReturn<T>] extends [infer Params, infer Return] ? [Params, Return] extends [[...(infer K), (error, result) => Any], Any] ? (...param) => Promise<R> : (...param) => Promise<Return> : never;

Converts a Function type that returns a Promise instead of the original return type.

If the Function takes a callback (e.g., (error, result) => void), it converts the Function to return a Promise with the callback result. Otherwise, it wraps the return type in a Promise.

Type Parameters

Type Parameter Description

T extends Fn

The Function type to convert.

Returns

A new Function type that returns a Promise.

Example

// Callback-style function:
// (a: string) => Promise<string>
type Promisified = FunctionPromisify<(a: string, callback: (error: Error, result: string) => void) => void>;

// Regular function:
// (a: string) => Promise<string>
type PromisifiedFn = FunctionPromisify<(a: string) => string>;