January 2020
Intermediate to advanced
470 pages
11h 13m
English
Let's enhance our logging function a bit by considering an adjustment. What happens to your log if the function throws an error? Fortunately, this is easy to solve. We just have to add a try/catch structure, as shown in the following code:
const addLogging2 = fn => (...args) => { console.log(`entering ${fn.name}: ${args}`); try { const valueToReturn = fn(...args); console.log(`exiting ${fn.name}: ${valueToReturn}`); return valueToReturn; } catch (thrownError) { console.log(`exiting ${fn.name}: threw ${thrownError}`); throw thrownError; }};
With this change, if the function threw an error, you'd also get an appropriate logging message, and the exception would be rethrown for processing.
Other changes to get ...
Read now
Unlock full access