feat(storage): add "end" method to storage for cleaning up resources when commands are finished
This commit is contained in:
parent
334e2099bb
commit
703e6f028a
15 changed files with 150 additions and 77 deletions
22
packages/cli/src/exec.ts
Normal file
22
packages/cli/src/exec.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import { toError } from './errors.js';
|
||||
|
||||
type Fn<Args extends any[], Result> = (...args: Args) => Result;
|
||||
type Result<T> = [value: T, error: undefined] | [value: undefined, error: Error];
|
||||
|
||||
/**
|
||||
* Execute a function and return a result tuple
|
||||
*
|
||||
* This is a helper function to make it easier to handle errors without the extra nesting of try/catch
|
||||
*/
|
||||
export const exec = async <Args extends any[], Return extends Promise<any>>(
|
||||
fn: Fn<Args, Return>,
|
||||
...args: Args
|
||||
): Promise<Result<Awaited<Return>>> => {
|
||||
try {
|
||||
const result = await fn(...args);
|
||||
|
||||
return [result, undefined];
|
||||
} catch (error) {
|
||||
return [undefined, toError(error)];
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue