Last active
January 15, 2019 16:40
-
-
Save DZuz14/a4a1e13432a8556738c8393134dfff27 to your computer and use it in GitHub Desktop.
Async Await Promises
This file contains hidden or 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
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