Brutkey

Kotes
@kotaro@kotaro.me

I was so foolish today. It all happened in just a few hours, but it’s already become an embarrassing memory.

In JavaScript, when you’re inside an async function and want to store JSON received from a server, you assign it like this:
const data = await response.json(). You do this because response.json() returns a Promise, and you can’t get the actual data without using await.

I didn’t forget that part. In fact, I was so careful that I even kept using
await on the data after storing it in a variable, like selectedMonsterData = await data. I thought I had a solid grasp of JavaScript fundamentals.

But when I called the async function that contains
fetch from a different button’s click event listener, I forgot to use await. To be precise, I forgot how to declare an event listener as async. So I never even thought to add await before calling the function that runs fetch.

Because of that, the front end didn’t wait for the necessary data after I clicked the button to send a POST request to the server, and that caused a null error.

Overconfidence can delay projects. My mistake today only cost me a day, but it made me reflect deeply.


rag. Gustavino Bevilacqua
@GustavinoBevilacqua@mastodon.cisti.org

@kotaro@kotaro.me

At least you found the reason by yourself, and this is greatly remarkable.

Kotes
@kotaro@kotaro.me

@GustavinoBevilacqua@mastodon.cisti.org No, the backend engineer on our project was actually the first one to point that out.

However, it took me quite a while to understand the error state he was describing.

"There's no way
I would handle a Promise without resolving it into actual data. After all, I've used await inside async functions countless times..."

I was extremely stubborn and set in my ways. I believed that if it were a Promise error, it would have been logged as such.

But in reality, that wasn't what he meant by the error at all. The problem was in how the
async function was being called from "outside" of it.