ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
23 lines (21 loc) • 662 B
text/typescript
import type { Callback } from '../util/callback';
/**
* Creates an async callback that invokes the provided callbacks in parallel.
* Undefined callbacks are skipped, and thrown or rejected callback errors are
* ignored.
*
* @param callbacks The callbacks to invoke for each event.
* @returns A callback that forwards each event to all callbacks and waits for
* them to settle.
*/
export function mergeCallbacks<EVENT>(
...callbacks: Array<Callback<EVENT> | undefined>
): Callback<EVENT> {
return async (event: EVENT) => {
await Promise.allSettled(
callbacks.map(async callback => {
await callback?.(event);
}),
);
};
}