Skip to content

Instantly share code, notes, and snippets.

Created February 22, 2017 14:02
Show Gist options
  • Save wesbos/1866f918824936ffb73d8fd0b02879b4 to your computer and use it in GitHub Desktop.
Save wesbos/1866f918824936ffb73d8fd0b02879b4 to your computer and use it in GitHub Desktop.
Simple Async/Await Example
// 🔥 Node 7.6 has async/await! Here is a quick run down on how async/await works
const axios = require('axios'); // promised based requests - like fetch()
function getCoffee() {
return new Promise(resolve => {
setTimeout(() => resolve('☕'), 2000); // it takes 2 seconds to make coffee
async function go() {
try {
// but first, coffee
const coffee = await getCoffee();
console.log(coffee); // ☕
// then we grab some data over an Ajax request
const wes = await axios('');
console.log(; // mediocre code
// many requests should be concurrent - don't slow things down!
// fire off three requests and save their promises
const wordPromise = axios('');
const userPromise = axios('');
const namePromise = axios('');
// await all three promises to come back and destructure the result into their own variables
const [word, user, name] = await Promise.all([wordPromise, userPromise, namePromise]);
console.log(,,; // cool, {...}, {....}
} catch (e) {
console.error(e); // 💩
Copy link

azza85 commented Apr 30, 2018

Thanks this is the best resource I have found!

Copy link

yyw794 commented Jun 6, 2018

simple is the power!

Copy link

Very nice example. Thanks

Copy link


Copy link

lol at the comments. but thanks!

Copy link

Awesome :)

Copy link

mskian commented Sep 4, 2018

Thanks :-)
it's Awesome 💯

Copy link

Thanks dude. God bless!

Copy link

McGern commented Sep 12, 2018

Thank you. Concise and easy to understand.

Copy link

eliojf commented Sep 12, 2018

Best exemple i found, thanks

Copy link

it's Awesome

Copy link

adamlutz commented Oct 9, 2018

Good Lord, thank you

Copy link


Copy link

Helped me understand the fundamentals

Copy link

🙏 Thank you!

Copy link


Copy link

Thank you

Copy link

mnts26 commented Jan 10, 2019

does it work on IOS 9,10 Safari?.

Copy link


Copy link

jzeron commented Jan 30, 2019

brilliant & funny :-) Thank you!

Copy link

Thank You! Clearest example I've found, no Bs just straight to the code!

Copy link

Excellent example!

Copy link


Copy link

Thanks mate!

Copy link

Very clear!, thanks

Copy link


Copy link

Thanks. Really simple straight forward example.

Copy link

good job! :)

Copy link

Great :)

Copy link

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment