Skip to content

Instantly share code, notes, and snippets.

@alperreha
Last active April 22, 2025 21:12
Show Gist options
  • Save alperreha/cfe829b68c3fc0196b3c3c2215bf31fe to your computer and use it in GitHub Desktop.
Save alperreha/cfe829b68c3fc0196b3c3c2215bf31fe to your computer and use it in GitHub Desktop.
a simple golang error returner like helper function in javascript https://www.npmjs.com/package/promise-like-go
// executeAsync is golang like function that returns [result,err] pair.
export async function executeAsync<T>(fn: () => Promise<T>): Promise<[T | null, any]> {
try {
const result = await fn();
return [result, null];
} catch (error) {
return [null, error];
}
}
// e.g. usage
// get supabase get posts table
const [post, error] = await executeAsync(async () =>
supabase.from('posts').select('*').eq('id', 1)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment