Created
March 25, 2016 20:55
-
-
Save pr1ntr/e283e318b9bcac99087a to your computer and use it in GitHub Desktop.
async / await vs promise
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
/** | |
* getSomeShit() | |
* returns Promise | |
* */ | |
function getSomeShit() { | |
return fetch('http://some.api/endpoint', {method:"GET", header:{'content-type': 'application/json' }}).then(r => r.json()) | |
} | |
//Promises | |
function gotTheShit() { | |
getSomeshit().then(json => { | |
//do something with json here | |
}).catch(fucked => { | |
throw new Error('shit is fucked dude'); | |
}) | |
} | |
//Async/Await | |
async function gotTheShit2() { | |
try { | |
let json = await getSomeShit(); | |
// do stuff with json like a normal blocking function | |
} catch (fucked) { | |
throw new Error('shit is fucked dude'); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment