Created
February 20, 2018 14:22
-
-
Save njlr/bdf11cf5c3891fbd5b42259b2b31757a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// try-catch style | |
// | |
// Advantages: | |
// 1. Familiar to OOP developers | |
// 2. Explicit error-handling code not required | |
// 3. Works well with async-await syntax | |
// | |
// Disadvantages: | |
// 1. Inefficient | |
// 2. Explicit error-handling code not required | |
// | |
const f = () => { | |
throw new Error('Something has gone wrong. '); | |
}; | |
try { | |
f(); | |
} catch (error) { | |
console.error(error); | |
} | |
// callback style | |
// | |
// Advantages: | |
// 1. Familiar to JavaScript developers | |
// 2. Explicit error-handling code is required | |
// | |
// Disadvantages: | |
// 1. Pyramid-of-doom / callback-hell | |
// 2. Explicit error-handling code is required | |
// 3. Does not work well with async-await syntax | |
// | |
const f = (callback) => { | |
callback(new Error('Something has gone wrong. '), null); | |
}; | |
f((error, result) => { | |
if (error) { | |
console.error(error); | |
} | |
}); | |
// functional-programming style | |
// | |
// Advantages: | |
// 1. Familiar to FP & Go developers | |
// 2. Explicit error-handling code is required | |
// 3. Works well with gradual type-systems | |
// 4. Works well with async-await syntax | |
// 5. Efficient | |
// 6. Encourages purity | |
// | |
// Disadvantages: | |
// 1. Explicit error-handling code is required | |
// 2. Can be unfamiliar to OOP developers | |
// | |
const f = () => { | |
return { | |
error: new Error('Something has gone wrong. '), | |
result: null | |
}; | |
}; | |
const { error, result } = f(); | |
if (error) { | |
console.error(error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment