Line data Source code
1 1 : export const RESULT_OK = Symbol();
2 1 : export const RESULT_ERROR = Symbol();
3 :
4 : export type Result<ValueType> =
5 : | { type: typeof RESULT_OK; value: ValueType }
6 : | { type: typeof RESULT_ERROR; error: any };
7 :
8 :
9 1 : export async function awaitAndSet<V>(v: Promise<V>, setResult: (result: Result<V>) => void) {
10 1 : try {
11 1 : const res = await v;
12 1 : setResult({
13 1 : type: RESULT_OK,
14 1 : value: res,
15 1 : })
16 1 : } catch (e: any) {
17 1 : setResult({
18 1 : type: RESULT_ERROR,
19 1 : error: e
20 1 : });
21 1 : }
22 1 : }
23 :
24 : export async function awaitAndSetOrNull<V>(v: Promise<V>, setResult: (result: V | null) => void) {
25 : try {
26 : const res = await v;
27 : setResult(res);
28 : } catch (e: any) {
29 : setResult(null)
30 : }
31 : }
|