Last active
May 1, 2025 18:35
-
-
Save Kcko/8dfa6f5005e3cd5f1ef722d5f3bdaaee 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
// https://devsmitra.medium.com/javascript-error-handling-just-got-a-whole-lot-easier-meet-the-safe-assignment-operator-c372d892d4ed | |
// without | |
async function getData() { | |
try { | |
const response = await fetch("https://api.example.com/data"); | |
const json = await response.json(); | |
return validationSchema.parse(json); | |
} catch (error) { | |
handleError(error); | |
} | |
} | |
// with | |
async function getData() { | |
const [fetchError, response]?= await fetch("https://api.example.com/data"); | |
if (fetchError) return handleFetchError(fetchError); | |
const [jsonError, json]?= await response.json(); | |
if (jsonError) return handleJsonError(jsonError); | |
const [validationError, data]?= validationSchema.parse(json); | |
if (validationError) return handleValidationError(validationError); | |
return data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment