Unhandled Rejection!

Adventures in Coding Errors

James Landry
3 min readOct 20, 2020

--

After spending countless hours of confusion as to why my code wasn’t working, only to find a missing “a” in one of my variables, it was recommended to me to add a Spell Checker plugin for my VS Code. Immediately, it helped me to catch several spelling issues I otherwise would have missed. I was elated with this newfound power (and a little annoyed that no one had recommended this to me before). So when my code broke once again about 15 minutes later, I was completely flummoxed. I carefully read through my code for the little squiggly lines that would show a spelling error or a missing closing bracket to no avail.

A few minutes with Google gave me little hope in solving my problems. According to the Mozilla Developer Network, Unhandled Rejections occur “when a JavaScript promise that has no rejection handler is rejected”. Seems straight forward enough, but why was my promise being rejected in the first place? ( a JavaScript Promise is an object that returns asynchronously that allows the rest of the code to run without this bit of code holding it up) I dove in to my async function and dissected it as best I could, but after 45 minutes, I knew I was over my head. No matter what I changed, I couldn’t get rid of the Unhandled Rejection error.

I finally reached out for help from a senior dev who also happened to be one of my former instructors for the software engineering boot camp I was attending at the time. (And the same who gave me the advice on the Spell Checker) “Help!” I said, “I have gone over everything I can think of and just cannot figure out this error!”

I walked him through what I was trying to do with my code, and where I thought my error might be. He took a look for about 30 seconds.

“It’s not your async function” he said.

“It’s not?” I was completely confused now.

“No. You wrote it fine. But look at the variable name you used.”

“Ok…” More confusion. It wasn’t a restricted name. What was wrong with it?

“You also assigned it further down in your code.”

“…” I am an idiot. “Thanks” I said. “I can’t believe I missed that.”

“Happens to the best of us” he said cheerfully.

What was it that he was doing that I was not to find the bug so quickly?

I recently came across an article from Daniel Anderson on Medium where he sums up the strategy perfectly. He said “Focus on the fundamentals, not the syntax”.

And this is exactly what my former instructor was doing when he looked at my code. He didn’t look for the spelling errors or the missing brackets. He looked at what my code was doing and he just followed the logic.

Since graduating from the bootcamp, the lessons I learned from my early errors have saved me countless hours on my current projects. While my bugs aren’t always spelling errors, or poorly named variables, (and with my experience I’m focusing more on being explicit with my variable names so hopefully those will continue to diminish) what once was taking me hours to debug takes me minutes as I walk through what my code does line by line I’m able to more clearly see where errors are happening and catch them quicker.

--

--