Skip to content

Instantly share code, notes, and snippets.

@reyronald
Created January 6, 2026 01:52
Show Gist options
  • Select an option

  • Save reyronald/d48ae89edf6767d3adb05ab18e17f6d0 to your computer and use it in GitHub Desktop.

Select an option

Save reyronald/d48ae89edf6767d3adb05ab18e17f6d0 to your computer and use it in GitHub Desktop.
// 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