Skip to content

Instantly share code, notes, and snippets.

@wesbos
Created February 22, 2017 14:02
Show Gist options
  • Select an option

  • Save wesbos/1866f918824936ffb73d8fd0b02879b4 to your computer and use it in GitHub Desktop.

Select an option

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('https://api.github.com/users/wesbos');
console.log(wes.data); // mediocre code
// many requests should be concurrent - don't slow things down!
// fire off three requests and save their promises
const wordPromise = axios('http://www.setgetgo.com/randomword/get.php');
const userPromise = axios('https://randomuser.me/api/');
const namePromise = axios('https://uinames.com/api/');
// 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(word.data, user.data, name.data); // cool, {...}, {....}
} catch (e) {
console.error(e); // 💩
}
}
go();
@mskian
Copy link
Copy Markdown

mskian commented Sep 4, 2018

Thanks :-)
it's Awesome 💯

@adamsdenniskariuki
Copy link
Copy Markdown

Thanks dude. God bless!

@McGern
Copy link
Copy Markdown

McGern commented Sep 12, 2018

Thank you. Concise and easy to understand.

@eliojf
Copy link
Copy Markdown

eliojf commented Sep 12, 2018

Best exemple i found, thanks

@Pioneer-Linzi
Copy link
Copy Markdown

it's Awesome

@adamlutz
Copy link
Copy Markdown

adamlutz commented Oct 9, 2018

Good Lord, thank you

@ToniMaunde
Copy link
Copy Markdown

pepethefrog

@AlistairHardy
Copy link
Copy Markdown

Helped me understand the fundamentals
tyvm!

@blaney83
Copy link
Copy Markdown

🙏 Thank you!

@lahaxearnaud
Copy link
Copy Markdown

thx

@Victor-Ugwueze
Copy link
Copy Markdown

Thank you

@mnts26
Copy link
Copy Markdown

mnts26 commented Jan 10, 2019

does it work on IOS 9,10 Safari?.

@Felix-Indoing
Copy link
Copy Markdown

thanks

@jzeron
Copy link
Copy Markdown

jzeron commented Jan 30, 2019

brilliant & funny :-) Thank you!

@georgecode
Copy link
Copy Markdown

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

@sean-xiao-zhao7
Copy link
Copy Markdown

Excellent example!

@CatalinAtun
Copy link
Copy Markdown

awesome

@Cleberw3b
Copy link
Copy Markdown

Thanks mate!

@agustinl
Copy link
Copy Markdown

Very clear!, thanks

@johndiego
Copy link
Copy Markdown

NICE =)

@SujanKumarK
Copy link
Copy Markdown

Thanks. Really simple straight forward example.

@emilcheva
Copy link
Copy Markdown

good job! :)

@sureshalagarsamy
Copy link
Copy Markdown

Great :)

@shabaoma
Copy link
Copy Markdown

Thank you very much.

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