ebs.getSafe
Executes a function and returns either its return value or, in the case of an error/exception, the undefined property.
ebs.getSafe allows you to handle function errors in a way that interferes as little as possible with the rest of your code execution.
Syntax
Copy
function ebs.getSafe<T>(fn: Function): T;
| Parameter | Type | Description |
|---|---|---|
fn
|
Function | Name of the function to be executed. |
Return Value
If the function passed as parameter executes successfully, its return value is returned. If the function passed as parameter generates an error or exception, the undefined property is returned.
Example
In this example:
- We check if the myEvaluation function returns an error.
- If the function returns an error, we log a message in the browser console.
- If the function runs successfully, we log the result in the browser console.
Copy
if (ebs.getSafe(myEvaluation) === undefined){
console.log('Evaluation failed. Skipping evaluation...')
}else{
console.log('Evaluated value: ' + myEvaluation())
}