Created
May 14, 2019 06:31
-
-
Save black-black-cat/3f31fc85b7b92b02e4c42ce047b46b71 to your computer and use it in GitHub Desktop.
Easier Error Handling Using Async/Await
This file contains 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
<!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> |
This file contains 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
/* todo: add styles */ |
This file contains 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://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