// 🔥 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(); |
This comment has been minimized.
This comment has been minimized.
awesome ! |
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks you! |
This comment has been minimized.
This comment has been minimized.
Hilarious! |
This comment has been minimized.
This comment has been minimized.
nice |
This comment has been minimized.
This comment has been minimized.
Simple. |
This comment has been minimized.
This comment has been minimized.
Thanks simple and useful |
This comment has been minimized.
This comment has been minimized.
简洁明了 |
This comment has been minimized.
This comment has been minimized.
Thanks! It helped to understand |
This comment has been minimized.
This comment has been minimized.
That very userful for me. Thanks you so much ! |
This comment has been minimized.
This comment has been minimized.
thanks a lot |
This comment has been minimized.
This comment has been minimized.
thanks! |
This comment has been minimized.
This comment has been minimized.
Thanks) |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
Awesome! |
This comment has been minimized.
This comment has been minimized.
Much appreciated :) |
This comment has been minimized.
This comment has been minimized.
This example is great! Thank you so much! |
This comment has been minimized.
This comment has been minimized.
As someone who is new to asyncawait topic, your example was very helpful. :) |
This comment has been minimized.
This comment has been minimized.
Thanks this is the best resource I have found! |
This comment has been minimized.
This comment has been minimized.
simple is the power! |
This comment has been minimized.
This comment has been minimized.
Very nice example. Thanks |
This comment has been minimized.
This comment has been minimized.
ty |
This comment has been minimized.
This comment has been minimized.
lol at the comments. but thanks! |
This comment has been minimized.
This comment has been minimized.
Awesome :) |
This comment has been minimized.
This comment has been minimized.
Thanks :-) |
This comment has been minimized.
This comment has been minimized.
Thanks dude. God bless! |
This comment has been minimized.
This comment has been minimized.
Thank you. Concise and easy to understand. |
This comment has been minimized.
This comment has been minimized.
Best exemple i found, thanks |
This comment has been minimized.
This comment has been minimized.
it's Awesome |
This comment has been minimized.
This comment has been minimized.
Good Lord, thank you |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Helped me understand the fundamentals |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
thx |
This comment has been minimized.
This comment has been minimized.
Thank you |
This comment has been minimized.
This comment has been minimized.
does it work on IOS 9,10 Safari?. |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
brilliant & funny :-) Thank you! |
This comment has been minimized.
This comment has been minimized.
Thank You! Clearest example I've found, no Bs just straight to the code! |
This comment has been minimized.
This comment has been minimized.
Excellent example! |
This comment has been minimized.
This comment has been minimized.
awesome |
This comment has been minimized.
This comment has been minimized.
Thanks mate! |
This comment has been minimized.
This comment has been minimized.
Very clear!, thanks |
This comment has been minimized.
This comment has been minimized.
NICE =) |
This comment has been minimized.
This comment has been minimized.
Thanks. Really simple straight forward example. |
This comment has been minimized.
This comment has been minimized.
good job! :) |
This comment has been minimized.
This comment has been minimized.
Great :) |
This comment has been minimized.
This comment has been minimized.
Thank you very much. |
This comment has been minimized.
отлично, хорошая работа