Skip to content

Instantly share code, notes, and snippets.

@pr1ntr
Created March 25, 2016 20:55
Show Gist options
  • Save pr1ntr/e283e318b9bcac99087a to your computer and use it in GitHub Desktop.
Save pr1ntr/e283e318b9bcac99087a to your computer and use it in GitHub Desktop.
async / await vs promise
/**
* 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