Skip to content

Instantly share code, notes, and snippets.

@DZuz14 DZuz14/async.js
Last active Jan 15, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.