Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Created May 14, 2019 06:31
Show Gist options
  • Save black-black-cat/3f31fc85b7b92b02e4c42ce047b46b71 to your computer and use it in GitHub Desktop.
Save black-black-cat/3f31fc85b7b92b02e4c42ce047b46b71 to your computer and use it in GitHub Desktop.
Easier Error Handling Using Async/Await
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello world!</h1>
<script src="sureThing.js"></script>
</body>
</html>
/* todo: add styles */
/**
* https://youtu.be/0iiZHlT0boc?t=582
*/
const sureThing = promise =>
promise
.then( result => ({ok: true, result}) )
.catch( error => ({ok: false, error}) )
const maybeCow = cow =>
new Promise( (success, failure) =>
cow === 'cow' ? success('it is a cow') : failure( new Error(`it's not a cow, it is: ${cow}`) ) )
const go = async () => {
let {ok, error, result} = await sureThing(maybeCow('cat'))
if (ok) {
} else {
console.log(error)
}
}
go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment