UNPKG

safety-number-one

Version:

Utility functions to work with Promises, functions and async functions safely by converting throws to Result Tuple types

21 lines 711 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runFnSafely = runFnSafely; const convert_unknown_catch_to_error_1 = require("convert-unknown-catch-to-error"); /** * Utility function run a synchronous function safely to prevent it from * throwing and returns a `ResultTuple`. * * ### Alternatively * 1. Run an async function with global util `runAsyncFnSafely`. * 1. Await a Promise with global util `awaitPromiseSafely`. */ function runFnSafely(fn, ...args) { try { return [null, fn(...args)]; } catch (e) { return [(0, convert_unknown_catch_to_error_1.convertUnknownCatchToError)(e), null]; } } //# sourceMappingURL=runFnSafely.js.map