Skip to content

Instantly share code, notes, and snippets.

@jmaicaaan
Last active March 31, 2021 13:11
Show Gist options
  • Save jmaicaaan/a56c5483a732a00c6044d7c23b2f054e to your computer and use it in GitHub Desktop.
Save jmaicaaan/a56c5483a732a00c6044d7c23b2f054e to your computer and use it in GitHub Desktop.
[Tutorial] Functional try catch
// mutable variable
const getData = (id) => {
if (!id) {
throw new Error('No id found');
}
return [1, 2, 3, 4, 5].filter(i => i % id);
};
const demo01 = () => {
let data = []
try {
data = getData(2); // [1, 3, 5]
} catch (error) {
console.log('error', error);
}
if (data.includes(1)) {
console.log(data)
}
}
// rabbit hole
const demo02 = () => {
try {
const data = getData(2); // [1, 3, 5]
if (data.includes('1')) {
console.log(data)
}
} catch (error) {
console.log('error', error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment