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();
@aneurysmjs

This comment has been minimized.

Copy link

@aneurysmjs aneurysmjs commented Mar 1, 2017

отлично, хорошая работа

@moeinrahimi

This comment has been minimized.

Copy link

@moeinrahimi moeinrahimi commented Jul 9, 2017

awesome !

@andreafalzetti

This comment has been minimized.

Copy link

@andreafalzetti andreafalzetti commented Jul 17, 2017

Thanks!

@rtorres90

This comment has been minimized.

Copy link

@rtorres90 rtorres90 commented Jul 28, 2017

Thanks you!

@pribilinskiy

This comment has been minimized.

Copy link

@pribilinskiy pribilinskiy commented Jul 31, 2017

Hilarious! 💯

@alersenkevich

This comment has been minimized.

Copy link

@alersenkevich alersenkevich commented Aug 7, 2017

nice

@androidfanatic

This comment has been minimized.

Copy link

@androidfanatic androidfanatic commented Sep 27, 2017

Simple.

@ilmoralito

This comment has been minimized.

Copy link

@ilmoralito ilmoralito commented Oct 10, 2017

Thanks simple and useful

@yeshimei

This comment has been minimized.

Copy link

@yeshimei yeshimei commented Nov 4, 2017

简洁明了

@masayannuu

This comment has been minimized.

Copy link

@masayannuu masayannuu commented Nov 17, 2017

Thanks! It helped to understand

@lamkent00

This comment has been minimized.

Copy link

@lamkent00 lamkent00 commented Nov 18, 2017

That very userful for me. Thanks you so much !

@lysandroc

This comment has been minimized.

Copy link

@lysandroc lysandroc commented Dec 8, 2017

thanks a lot

@foobar8675

This comment has been minimized.

Copy link

@foobar8675 foobar8675 commented Jan 17, 2018

thanks!

@orionych

This comment has been minimized.

Copy link

@orionych orionych commented Feb 2, 2018

Thanks)

@netdesignr

This comment has been minimized.

Copy link

@netdesignr netdesignr commented Feb 9, 2018

Thank you!

@korotindev

This comment has been minimized.

Copy link

@korotindev korotindev commented Feb 15, 2018

Awesome!

@gnzg

This comment has been minimized.

Copy link

@gnzg gnzg commented Apr 9, 2018

Much appreciated :)

@achandrashekhar

This comment has been minimized.

Copy link

@achandrashekhar achandrashekhar commented Apr 16, 2018

This example is great! Thank you so much!

@ewliang

This comment has been minimized.

Copy link

@ewliang ewliang commented Apr 18, 2018

As someone who is new to asyncawait topic, your example was very helpful. :)

@azza85

This comment has been minimized.

Copy link

@azza85 azza85 commented Apr 30, 2018

Thanks this is the best resource I have found!

@yyw794

This comment has been minimized.

Copy link

@yyw794 yyw794 commented Jun 6, 2018

simple is the power!

@satyapendem

This comment has been minimized.

Copy link

@satyapendem satyapendem commented Jun 7, 2018

Very nice example. Thanks

@alvarosacari

This comment has been minimized.

Copy link

@alvarosacari alvarosacari commented Jul 17, 2018

ty

@johnnyboxdev

This comment has been minimized.

Copy link

@johnnyboxdev johnnyboxdev commented Jul 20, 2018

lol at the comments. but thanks!

@jakeeqsb

This comment has been minimized.

Copy link

@jakeeqsb jakeeqsb commented Jul 24, 2018

Awesome :)

@mskian

This comment has been minimized.

Copy link

@mskian mskian commented Sep 4, 2018

Thanks :-)
it's Awesome 💯

@adamsdenniskariuki

This comment has been minimized.

Copy link

@adamsdenniskariuki adamsdenniskariuki commented Sep 10, 2018

Thanks dude. God bless!

@McGern

This comment has been minimized.

Copy link

@McGern McGern commented Sep 12, 2018

Thank you. Concise and easy to understand.

@eliojf

This comment has been minimized.

Copy link

@eliojf eliojf commented Sep 12, 2018

Best exemple i found, thanks

@Pioneer-Linzi

This comment has been minimized.

Copy link

@Pioneer-Linzi Pioneer-Linzi commented Sep 27, 2018

it's Awesome

@adamlutz

This comment has been minimized.

Copy link

@adamlutz adamlutz commented Oct 9, 2018

Good Lord, thank you

@ToniMaunde

This comment has been minimized.

Copy link

@ToniMaunde ToniMaunde commented Nov 13, 2018

pepethefrog

@AlistairHardy

This comment has been minimized.

Copy link

@AlistairHardy AlistairHardy commented Dec 4, 2018

Helped me understand the fundamentals
tyvm!

@blaney83

This comment has been minimized.

Copy link

@blaney83 blaney83 commented Dec 10, 2018

🙏 Thank you!

@lahaxearnaud

This comment has been minimized.

Copy link

@lahaxearnaud lahaxearnaud commented Dec 12, 2018

thx

@Victor-Ugwueze

This comment has been minimized.

Copy link

@Victor-Ugwueze Victor-Ugwueze commented Jan 3, 2019

Thank you

@mnts26

This comment has been minimized.

Copy link

@mnts26 mnts26 commented Jan 10, 2019

does it work on IOS 9,10 Safari?.

@Felix-Indoing

This comment has been minimized.

Copy link

@Felix-Indoing Felix-Indoing commented Jan 29, 2019

thanks

@jzeron

This comment has been minimized.

Copy link

@jzeron jzeron commented Jan 30, 2019

brilliant & funny :-) Thank you!

@georgecode

This comment has been minimized.

Copy link

@georgecode georgecode commented Feb 1, 2019

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

@sean-xiao-zhao7

This comment has been minimized.

Copy link

@sean-xiao-zhao7 sean-xiao-zhao7 commented Feb 13, 2019

Excellent example!

@CatalinAtun

This comment has been minimized.

Copy link

@CatalinAtun CatalinAtun commented Mar 3, 2019

awesome

@Cleberw3b

This comment has been minimized.

Copy link

@Cleberw3b Cleberw3b commented Mar 11, 2019

Thanks mate!

@agustinl

This comment has been minimized.

Copy link

@agustinl agustinl commented Jun 22, 2019

Very clear!, thanks

@johndiego

This comment has been minimized.

Copy link

@johndiego johndiego commented Aug 20, 2019

NICE =)

@SujanKumarK

This comment has been minimized.

Copy link

@SujanKumarK SujanKumarK commented Oct 9, 2019

Thanks. Really simple straight forward example.

@emilcheva

This comment has been minimized.

Copy link

@emilcheva emilcheva commented Nov 19, 2019

good job! :)

@sureshalagarsamy

This comment has been minimized.

Copy link

@sureshalagarsamy sureshalagarsamy commented Feb 27, 2020

Great :)

@shabaoma

This comment has been minimized.

Copy link

@shabaoma 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
You can’t perform that action at this time.