-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.ts
67 lines (61 loc) · 1.71 KB
/
go.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* @file go.ts
* @copyright 2020 Brandon Kalinowski @brandonkal
* @description Simplified async error handling in TypeScript.
* @license MIT
*/
/** Get the inner type of a Promise */
export type PromiseType<T extends Promise<any>> = T extends Promise<infer U>
? U
: never
export type Arguments<T> = T extends (...args: infer U) => infer R ? U : never
export type ReplaceReturn<T, TNewReturn> = (...a: Arguments<T>) => TNewReturn
type Fn = (...args: any[]) => any
export type Go<E extends Error, T extends Fn> = {
res: PromiseType<ReturnType<T>>
err: E | undefined
}
/**
* Go is a function that accepts a single function. It will execute it async and return the result in the
* { res, err } format popularized by Golang. This simplifies error handling because you will never have to write a try-catch block.
* Go Captures any thrown errors and returns them as values.
*
* Example:
* ```
* const main = async () => {
* const cmd = await go(() => doAsyncThing(param))
* if (cmd.err) {
* // handle error
* }
* console.log(cmd.res)
* }
* main().catch((e) => console.error(e))
* ```
*/
async function go<E extends Error, T extends Fn>(fn: T): Promise<Go<E, T>> {
let result
try {
result = await fn()
const err = result instanceof Error ? result : undefined
let output
if (result instanceof Object && result !== null) {
output = Object.create(result)
} else {
output = Object.create({})
}
output.err = err
output.res = result
return output
} catch (err) {
let output
if (result instanceof Object && result !== null) {
output = Object.create(result)
} else {
output = Object.create({})
}
output.res = undefined
output.err = err
return output
}
}
export default go