UNPKG

@sounisi5011/cli-utils-top-level-await

Version:

If the async function fails, set the process exit code to 1 and output the error to stderr

28 lines (26 loc) 780 B
export async function awaitMainFn(mainFnOrValue: (() => PromiseLike<void> | void) | PromiseLike<void>): Promise<void> { const mainFn = typeof mainFnOrValue === 'function' ? mainFnOrValue : () => mainFnOrValue; try { await mainFn(); } catch (error) { if ( typeof process.exitCode !== 'number' || Number.isNaN(process.exitCode) || process.exitCode % 256 === 0 ) { process.exitCode = 1; } console.error(error); } } export { awaitMainFn as awaitFn, awaitMainFn as awaitFunc, awaitMainFn as awaitFunction, awaitMainFn as awaitMainFunc, awaitMainFn as awaitMainFunction, awaitMainFn as topLevelAwait, awaitMainFn as topLevelAwaitCli, };