try-catch-expression
v1.0.8
Published
try-catch-expression provides try..catch as expression.
Downloads
77
Readme
try-catch-expression
try-catch-expression provides try..catch as expression.
Status
| Category | Status | | ---------------- | --------------------------------------------------------------------------------------------------- | | Version | | | Dependencies | | | Dev dependencies | | | Build | | | License | |
Installation
$ npm install try-catch-expression
Rationale
For handling errors, JavaScript and TypeScript provide the try
/ catch
statement. While it is highly useful, it sometimes makes your code hard to read, e.g. if you want to assign a variable and have a fallback in the catch
clause:
let result;
try {
result = getValue();
} catch {
result = getDefaultValue();
}
console.log(result);
For this (and other) scenarios it would be helpful it try
/ catch
was also available as an expression, so that you could write your code in a more convenient way:
const result = try {
return getValue();
} catch {
return getDefaultValue();
}
console.log(result);
Unfortunately, neither JavaScript nor TypeScript support this. That's what this module adds.
Quick start
First you need to add a reference to try-catch-expression in your application:
const { tryCatch, tryFinally, tryCatchFinally } = require('try-catch-expression');
If you use TypeScript, use the following code instead:
import { tryCatch, tryFinally, tryCatchFinally } from 'try-catch-expression';
To use a simple try
/ catch
, use the tryCatch
function and pass a potentially failing function as well as a function that acts as a catch clause. In that function, you may return a value that is then also returned by the surrounding tryCatch
function, or re-throw.
const result = tryCatch(
() => {
// Try something...
},
ex => {
// Return a default value or re-throw the exception.
}
);
This means that the variable result
will then either contain the return value of the try
function, in case it succeeds. If it fails, the value from the catch
function is returned. Any error thrown in the catch function will be passed through to the enclosing scope.
Using finally
Besides tryCatch
, there are also tryCatchFinally
and tryFinally
available:
const result = tryCatchFinally(
() => {
// Try something...
},
ex => {
// Return a default value or re-throw the exception.
},
() => {
// Clean up.
}
);
const result = tryFinally(
() => {
// Try something...
},
() => {
// Clean up.
}
);
Note that the return value of the finally function will be discarded, to avoid confusing behavior.
Running quality assurance
To run quality assurance for this module use roboter:
$ npx roboter