Skip to content

Commit

Permalink
Add whenPromiseSettles function
Browse files Browse the repository at this point in the history
Copied from the tests. We’ll use it for bridging between promise-using
methods and callback-using methods as we start changing the codebase to
use promises internally.
  • Loading branch information
lawrence-forooghian committed Jan 29, 2024
1 parent dfaa6ba commit cd6c2d8
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/common/lib/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,22 @@ export function promisify<T>(ob: Record<string, any>, fnName: string, args: IArg
});
}

/**
* Uses a callback to communicate the result of a `Promise`. The first argument passed to the callback will be either an error (when the promise is rejected) or `null` (when the promise is fulfilled). In the case where the promise is fulfilled, the resulting value will be passed to the callback as a second argument.
*/
export function whenPromiseSettles<T, E = unknown>(
promise: Promise<T>,
callback?: (err: E | null, result?: T) => void
) {
promise
.then((result) => {
callback?.(null, result);
})
.catch((err) => {
callback?.(err);
});
}

export function decodeBody<T>(body: unknown, MsgPack: MsgPack | null, format?: Format | null): T {
if (format == 'msgpack') {
if (!MsgPack) {
Expand Down

0 comments on commit cd6c2d8

Please sign in to comment.