CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
attemptify.ts37 linesDownload Raw Back to src
1 2/* MAIN */3 4//FIXME: The return type of these functions is wrong, it doesn't account for returning "undefined", but a correct type cannot be written because generics cannot be extended properly, it seems5 6const attemptifyAsync = <FN extends Function> ( fn: FN, onError: (( error: unknown ) => undefined) ): FN => {7 8  return function attemptified ( ...args: any ): any {9 10    return fn.apply ( undefined, args ).catch ( onError );11 12  } as any;13 14};15 16const attemptifySync = <FN extends Function> ( fn: FN, onError: (( error: unknown ) => undefined) ): FN => {17 18  return function attemptified ( ...args: any ): any {19 20    try {21 22      return fn.apply ( undefined, args );23 24    } catch ( error: unknown ) {25 26      return onError ( error );27 28    }29 30  } as any;31 32};33 34/* EXPORT */35 36export {attemptifyAsync, attemptifySync};37 
basant307/AI_Governance_Project · CoolFace