Last active
April 22, 2025 21:12
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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