Day 6:
Lots of different software projects require coordination with different services. Whenever you make a request to a service that you need a response from, you're going to need to wait to hear back from it. How you handle that in code is one of the core ideas of asynchronous programming. While I was working on my micropub endpoint, which would allow me to post on this site from other places, I came across a strange bug. Normally, I try to write my async JavaScript code using async-await style syntax, which handles a lot of the trickiness of async programming for you. Essentially, this feature of JS allows you to write async code almost the same way you’d write your regular synchronous code. This “almost” part is where things can get tricky, and where I ran into my error.
I was trying to get my server to upload a bunch of different photos I had stored in an array to an image hosting service. I needed to go through each photo and handle it before moving onto the next step. To do so, I decided to use the .forEach method on my array, as it allows me to iterate over each photo with ease. Yet my code wasn’t working at all like I wanted it to! I was using `await` on each request and the loop wouldn’t wait while the request was happening, like you’d expect to happen in synchronous code. After a quick search, I came across this article and that’s when I found out: the .forEach method doesn’t support await. Silly me had run into one of those edge cases where async-await code doesn’t work like you’d expect. I hope I don’t make that same mistake again and I definitely learned something new about a language I thought I knew so well. Debugging is always a humbling experience for me! Until tomorrow!