safety-number-one
Version:
Utility functions to work with Promises, functions and async functions safely by converting throws to Result Tuple types
18 lines • 561 B
JavaScript
import { convertUnknownCatchToError } from "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`.
*/
export function runFnSafely(fn, ...args) {
try {
return [null, fn(...args)];
}
catch (e) {
return [convertUnknownCatchToError(e), null];
}
}
//# sourceMappingURL=runFnSafely.js.map