Assignment: failsafe callback
Provide a function failSafe that - in curried style - takes:
- a default value
- a callback function
- an argument to the callback function that returns whatever the callback function returns given its argument or the default value if the callback function throws an Error.
💡
Note: see how the tests make use of failSafe.
Solution
const failSafe = defaultValue => callback => argument => {
try {
return callback(argument);
} catch (error) {
return defaultValue;
}
};Your solution will be tested against:
const doError = x => {
throw new Error()
};
const errorCount = failSafe(1)(doError);
failSafe(false)(x => x)(true) && failSafe(true)(doError)(null) && errorCount(null) === 1