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

commented Mar 1, 2017

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

@moeinrahimi

This comment has been minimized.

Copy link

commented Jul 9, 2017

awesome !

@andreafalzetti

This comment has been minimized.

Copy link

commented Jul 17, 2017

Thanks!

@rtorres90

This comment has been minimized.

Copy link

commented Jul 28, 2017

Thanks you!

@pribilinskiy

This comment has been minimized.

Copy link

commented Jul 31, 2017

Hilarious! 💯

@alersenkevich

This comment has been minimized.

Copy link

commented Aug 7, 2017

nice

@androidfanatic

This comment has been minimized.

Copy link

commented Sep 27, 2017

Simple.

@ilmoralito

This comment has been minimized.

Copy link

commented Oct 10, 2017

Thanks simple and useful

@yeshimei

This comment has been minimized.

Copy link

commented Nov 4, 2017

简洁明了

@masayannuu

This comment has been minimized.

Copy link

commented Nov 17, 2017

Thanks! It helped to understand

@lamkent00

This comment has been minimized.

Copy link

commented Nov 18, 2017

That very userful for me. Thanks you so much !

@Lysandroc

This comment has been minimized.

Copy link

commented Dec 8, 2017

thanks a lot

@foobar8675

This comment has been minimized.

Copy link

commented Jan 17, 2018

thanks!

@orionych

This comment has been minimized.

Copy link

commented Feb 2, 2018

Thanks)

@netdesignr

This comment has been minimized.

Copy link

commented Feb 9, 2018

Thank you!

@soxat

This comment has been minimized.

Copy link

commented Feb 15, 2018

Awesome!

@gnzg

This comment has been minimized.

Copy link

commented Apr 9, 2018

Much appreciated :)

@achandrashekhar

This comment has been minimized.

Copy link

commented Apr 16, 2018

This example is great! Thank you so much!

@ewliang

This comment has been minimized.

Copy link

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

commented Apr 30, 2018

Thanks this is the best resource I have found!

@yyw794

This comment has been minimized.

Copy link

commented Jun 6, 2018

simple is the power!

@satyapendem

This comment has been minimized.

Copy link

commented Jun 7, 2018

Very nice example. Thanks

@alvarosacari

This comment has been minimized.

Copy link

commented Jul 17, 2018

ty

@johnnyboxdev

This comment has been minimized.

Copy link

commented Jul 20, 2018

lol at the comments. but thanks!

@jakeeqsb

This comment has been minimized.

Copy link

commented Jul 24, 2018

Awesome :)

@mskian

This comment has been minimized.

Copy link

commented Sep 4, 2018

Thanks :-)
it's Awesome 💯

@adamsdenniskariuki

This comment has been minimized.

Copy link

commented Sep 10, 2018

Thanks dude. God bless!

@McGern

This comment has been minimized.

Copy link

commented Sep 12, 2018

Thank you. Concise and easy to understand.

@eliojf

This comment has been minimized.

Copy link

commented Sep 12, 2018

Best exemple i found, thanks

@Pioneer-Linzi

This comment has been minimized.

Copy link

commented Sep 27, 2018

it's Awesome

@adamlutz

This comment has been minimized.

Copy link

commented Oct 9, 2018

Good Lord, thank you

@ToniMaunde

This comment has been minimized.

Copy link

commented Nov 13, 2018

pepethefrog

@AlistairHardy

This comment has been minimized.

Copy link

commented Dec 4, 2018

Helped me understand the fundamentals
tyvm!

@blaney83

This comment has been minimized.

Copy link

commented Dec 10, 2018

🙏 Thank you!

@lahaxearnaud

This comment has been minimized.

Copy link

commented Dec 12, 2018

thx

@Victor-Ugwueze

This comment has been minimized.

Copy link

commented Jan 3, 2019

Thank you

@mnts26

This comment has been minimized.

Copy link

commented Jan 10, 2019

does it work on IOS 9,10 Safari?.

@Felix-Indoing

This comment has been minimized.

Copy link

commented Jan 29, 2019

thanks

@jzeron

This comment has been minimized.

Copy link

commented Jan 30, 2019

brilliant & funny :-) Thank you!

@georgecode

This comment has been minimized.

Copy link

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

commented Feb 13, 2019

Excellent example!

@CatalinaCampos

This comment has been minimized.

Copy link

commented Mar 3, 2019

awesome

@Cleberw3b

This comment has been minimized.

Copy link

commented Mar 11, 2019

Thanks mate!

@agustinl

This comment has been minimized.

Copy link

commented Jun 22, 2019

Very clear!, thanks

@johndiego

This comment has been minimized.

Copy link

commented Aug 20, 2019

NICE =)

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.