safe-func-safe-prom
v1.1.1
Published
Make both functions and promises more functional by handling try-catch/.catch, but letting native exceptions through
Downloads
2
Maintainers
Readme
safe-func-safe-prom
This module quickly turned into tri-fp which is the active library for this functionality. You can:
import { tri as safe, triP as safeP } from "tri-fp";
...if you like the names in this module.
- Make both functions and promises more functional by removing the need for try-catch.
- Handle application errors and native exceptions differently.
Wrap a function in safe
, or a promise in safeP
, to transform all outcomes except native exceptions into an error-first pair. Don't use try-catch
or .catch
.
import { safe, safeP } from "safe-func-safe-prom";
// Sync
const [error, result] = safe(myFunction)(arg1, arg2);
// Async, direct promise
const [error, result] = await safeP(myPromise);
// Async, promise returning function
const [error, result] = await safeP(myPromiseReturningFunc)(arg1, arg2);
// Native errors/exceptions will still throw. Don't catch these; Find your bug.
Motivation
- https://dev.to/craigmichaelmartin/the-problem-with-promises-in-javascript-5h46
- https://medium.com/better-programming/js-reliable-fdea261012ee
- https://medium.com/@gunar/async-control-flow-without-exceptions-nor-monads-b19af2acc553