Skip to content

Instantly share code, notes, and snippets.

@DZuz14
Last active January 15, 2019 16:40
Show Gist options
  • Save DZuz14/a4a1e13432a8556738c8393134dfff27 to your computer and use it in GitHub Desktop.
Save DZuz14/a4a1e13432a8556738c8393134dfff27 to your computer and use it in GitHub Desktop.
Async Await Promises
function someCoolPromise() {
return new Promise((resolve, reject) => {
// You can do anything you want in here for as long as you want.
// Then just call the resolve function when you are ready.
for(let i = 0;i < 10000; i++) {
// weeeee
}
resolve({
greeting: "Fuck you Dan."
});
// reject("Seriously...Fuck you Dan.");
});
}
async function someCoolShit() {
try {
const res = await someCoolPromise(); // Nothing below will run until this returns a resolved promise
console.log(res) // { greeting: "Fuck you Dan," }
console.log(res.greeting) // "Fuck you Dan."
} catch(err) {
// Anything after the res variable above will be skipped, and anything in this block will be executed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment