refactor(core/stdlib): streamline tryIt function with improved promise handling
This commit is contained in:
@@ -4,6 +4,9 @@ export type TryItReturn<Return> = Return extends Promise<any>
|
||||
? Promise<{ error: Error; data: undefined } | { error: undefined; data: Awaited<Return> }>
|
||||
: { error: Error; data: undefined } | { error: undefined; data: Return };
|
||||
|
||||
function onResolve(data: any) { return { error: undefined, data }; }
|
||||
function onReject(error: any) { return { error, data: undefined }; }
|
||||
|
||||
/**
|
||||
* @name tryIt
|
||||
* @category Async
|
||||
@@ -29,9 +32,7 @@ export function tryIt<Args extends any[], Return>(
|
||||
const result = fn(...args);
|
||||
|
||||
if (isPromise(result))
|
||||
return result
|
||||
.then((value) => ({ error: undefined, data: value }))
|
||||
.catch((error) => ({ error, data: undefined })) as TryItReturn<Return>;
|
||||
return result.then(onResolve).catch(onReject) as TryItReturn<Return>;
|
||||
|
||||
return { error: undefined, data: result } as TryItReturn<Return>;
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user