🎙️
8

The day I realized I was nesting functions wrong in JavaScript

I spent 3 months writing callbacks inside callbacks inside callbacks for a weather app project. Then a friend glanced at my code and said 'why don't you just use async/await?' I had no idea what that was. Now I'm wondering if learning the 'wrong' way first made me understand the better way more deeply. Did anyone else hit a moment where you realized your whole method was backward?
3 comments

Log in to join the discussion

Log In
3 Comments
grantf73
grantf731mo ago
Oh man, callback hell is basically a rite of passage isn't it? I remember literally wrapping a setTimeout inside another setTimeout because I thought that's how you controlled the order of API calls. My code looked like a sideways Christmas tree. When I finally discovered async/await I felt like I'd been building furniture with a hammer and someone just handed me a power drill. Sure the hammer works but why did I make it so hard on myself?
8
williamb29
williamb291mo ago
Wait, you went three months before someone told you about async/await? How did you even get the weather app working with all those nested callbacks?
2
charles442
charles44224d ago
I had almost the exact same moment with a calculator app I was building... just layers of if statements inside for loops inside more if statements. A buddy looked at my screen and asked if I'd ever heard of switch cases or ternary operators, and my mind was blown right there in the coffee shop. @williamb29 makes a good point though, three months is a long time to be in the dark... I only lasted about three weeks before someone rescued me from my own code. The sideways Christmas tree comparison is perfect, I actually printed out one of my old functions once and it looked like a staircase from an M.C. Escher painting.
2