Created
January 6, 2026 01:52
-
-
Save reyronald/d48ae89edf6767d3adb05ab18e17f6d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| export function promisifycall<T extends (...args: any) => any>(callback: T) { | |
| type Request = Parameters<T>[0] | |
| type _Metadata = Parameters<T>[1] | |
| type Callback = Parameters<T>[3] | |
| type Response = Parameters<Callback>[1] | |
| type ReturnTuple = [grpc.ServiceError, null] | [null, Response] | |
| return { | |
| call: (client: grpc.Client, req: Request) => { | |
| return new Promise<Response>((resolve, reject) => { | |
| const boundCallback = callback.bind(client) | |
| boundCallback(req, md, (err: grpc.ServiceError | null, res: Response) => { | |
| if (err) { | |
| reject(err) | |
| } else { | |
| resolve(res) | |
| } | |
| }) | |
| }) | |
| }, | |
| callSafe: (client: grpc.Client, req: Request) => { | |
| return new Promise<ReturnTuple>((resolve) => { | |
| const boundCallback = callback.bind(client) | |
| boundCallback(req, md, (err: grpc.ServiceError | null, res: Response) => { | |
| resolve(err ? ([err, null] as const) : ([null, res] as const)) | |
| }) | |
| }) | |
| }, | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment