Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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();
@azza85
Copy link

azza85 commented Apr 30, 2018

Thanks this is the best resource I have found!

@yyw794
Copy link

yyw794 commented Jun 6, 2018

simple is the power!

@satyapendem
Copy link

satyapendem commented Jun 7, 2018

Very nice example. Thanks

@alvarosacari
Copy link

alvarosacari commented Jul 17, 2018

ty

@johnnyboxdev
Copy link

johnnyboxdev commented Jul 20, 2018

lol at the comments. but thanks!

@jakeeqsb
Copy link

jakeeqsb commented Jul 24, 2018

Awesome :)

@mskian
Copy link

mskian commented Sep 4, 2018

Thanks :-)
it's Awesome 💯

@adamsdenniskariuki
Copy link

adamsdenniskariuki commented Sep 10, 2018

Thanks dude. God bless!

@McGern
Copy link

McGern commented Sep 12, 2018

Thank you. Concise and easy to understand.

@eliojf
Copy link

eliojf commented Sep 12, 2018

Best exemple i found, thanks

@Pioneer-Linzi
Copy link

Pioneer-Linzi commented Sep 27, 2018

it's Awesome

@adamlutz
Copy link

adamlutz commented Oct 9, 2018

Good Lord, thank you

@ToniMaunde
Copy link

ToniMaunde commented Nov 13, 2018

pepethefrog

@AlistairHardy
Copy link

AlistairHardy commented Dec 4, 2018

Helped me understand the fundamentals
tyvm!

@blaney83
Copy link

blaney83 commented Dec 10, 2018

🙏 Thank you!

@lahaxearnaud
Copy link

lahaxearnaud commented Dec 12, 2018

thx

@Victor-Ugwueze
Copy link

Victor-Ugwueze commented Jan 3, 2019

Thank you

@mnts26
Copy link

mnts26 commented Jan 10, 2019

does it work on IOS 9,10 Safari?.

@Felix-Indoing
Copy link

Felix-Indoing commented Jan 29, 2019

thanks

@jzeron
Copy link

jzeron commented Jan 30, 2019

brilliant & funny :-) Thank you!

@georgecode
Copy link

georgecode commented Feb 1, 2019

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

@sean-xiao-zhao7
Copy link

sean-xiao-zhao7 commented Feb 13, 2019

Excellent example!

@CatalinAtun
Copy link

CatalinAtun commented Mar 3, 2019

awesome

@Cleberw3b
Copy link

Cleberw3b commented Mar 11, 2019

Thanks mate!

@agustinl
Copy link

agustinl commented Jun 22, 2019

Very clear!, thanks

@johndiego
Copy link

johndiego commented Aug 20, 2019

NICE =)

@SujanKumarK
Copy link

SujanKumarK commented Oct 9, 2019

Thanks. Really simple straight forward example.

@emilcheva
Copy link

emilcheva commented Nov 19, 2019

good job! :)

@sureshalagarsamy
Copy link

sureshalagarsamy commented Feb 27, 2020

Great :)

@shabaoma
Copy link

shabaoma commented Apr 17, 2020

Thank you very much.

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