Learning Async/Await in JavaScript: Simplifying Code with Async/Await

Just wrapped up my 3rd JavaScript project - a Random Quote Generator! 🎲 This one was different. Not because it's complex (it's actually pretty simple), but because I finally *got* async/await. I've been reading about Promises and .then() chains for weeks. Understood them conceptually, but they always felt... messy? Like I was fighting with the syntax instead of just writing code. Then I rebuilt this project using async/await and something clicked. The code just reads like normal code. Top to bottom. No nesting. Clean error handling. It finally makes sense. Here's what changed for me: Before (with .then chains): fetch(url)  .then(response => response.json())  .then(data => displayQuote(data))  .catch(error => handleError(error)); After (with async/await): async function getQuote() {  try {   const response = await fetch(url);   const data = await response.json();   displayQuote(data);  } catch (error) {   handleError(error);  } } Same functionality. Way easier to read. The debugging moment that taught me the most: Spent 15 minutes wondering why my quote wasn't displaying. Kept getting "undefined." Turns out the API returns an array, not an object. So data.quote didn't work. But data[0].quote did. Simple fix. But it taught me to always console.log() API responses first before assuming their structure. Built in about an hour. Learned way more than an hour's worth. Small projects. Real learning. 🌐 Live: https://lnkd.in/gsf3dvfe 💻 Code: https://lnkd.in/gt2mwRFH #JavaScript #AsyncAwait #WebDevelopment #100DaysOfCode #LearningInPublic

  • Screenshot of a Random Quote Generator built with Vanilla JavaScript displaying a motivational quote and a button to generate a new quote.

Really liked the way you explained your learning process here .Keep going!

Like
Reply

To view or add a comment, sign in

Explore content categories