November 2017
Intermediate to advanced
386 pages
9h 22m
English
Let's enhance our logging function a bit, by considering a needed adjustment. What happens to your log if the function throws an error? Fortunately, that's easy to solve. We just have to add some 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; }};
Other changes would be up to you -- adding date and time data, enhancing the way parameters are listed, and so on. However, our implementation still has an important defect; let's make it better.