Add try-async method
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
export * from './event-emitter.ts';
|
||||
export * from './exponential-backoff.ts';
|
||||
export * from './extended-json.ts';
|
||||
export * from './misc.ts';
|
||||
export * from './script.ts';
|
||||
export * from './sse-session/index.ts';
|
||||
export * from './template/errors.ts';
|
||||
|
||||
15
source/misc.ts
Normal file
15
source/misc.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Tries to execute an async function and handles any errors that occur.
|
||||
* @param fn - The function to execute.
|
||||
* @param onError - The callback to call if the function fails.
|
||||
* @returns The result of the function.
|
||||
*/
|
||||
export const tryAsync = async (fn: () => unknown, onError?: (error: Error) => void): Promise<void> => {
|
||||
try {
|
||||
await fn();
|
||||
} catch (error) {
|
||||
const errorInstance = error instanceof Error ? error : new Error(`${error}`);
|
||||
|
||||
onError?.(errorInstance);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user