UNPKG

assistant-stream

Version:

Streaming utilities for AI assistants

24 lines (22 loc) • 585 B
export function withPromiseOrValue<T>( callback: () => T | PromiseLike<T>, thenHandler: (value: T) => PromiseLike<void> | void, catchHandler: (error: unknown) => PromiseLike<void> | void, ): PromiseLike<void> | void { let value: T; try { const promiseOrValue = callback(); if ( typeof promiseOrValue === "object" && promiseOrValue !== null && "then" in promiseOrValue ) { return promiseOrValue.then(thenHandler, catchHandler); } value = promiseOrValue; } catch (e) { catchHandler(e); return; } thenHandler(value); }