Last active
March 31, 2021 13:11
-
-
Save jmaicaaan/a56c5483a732a00c6044d7c23b2f054e to your computer and use it in GitHub Desktop.
[Tutorial] Functional try catch
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
// 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