So your RSS feed generator went down...
My favorite thing to wake up to on a lovely Monday morning.
I woke up, worked out, then took a long relaxing shower. I drank my morning Blue Razz Bang, punched in my pin, and got on my machine to begin the day’s work.
For no good reason, I felt the need to check my .dev domain to see how everything was working. As it turned out, the Universe had decided I was going to have a bad day.
The problem I was faced with
If you are unfamiliar with RSS, it’s basically XML for accessing constantly changing data from a site. In this case, it was my Medium RSS feed to give me access to my most recently published content on the site.
I was using the popular RSS feed generator feed2js to grab and style my content, then organizing it how I liked in JavaScript (relevant code here). It wasn’t the prettiest thing in the world, but it got the job done.
So when I opened my site on Monday morning, expecting to see the above, you can bet I got a shock when I saw this instead:
Naturally I opened the console and reloaded the page to see the requests scroll across the Network tab, and lo and behold, the feed2js request had a 404 status. So I took a big swig of the Bang, put on my big developer boots, and got to work.
I had just made this commit on my .com domain, implementing a more advanced retrieval method unique to the Medium API, so I was in luck. I took that general logic and distilled it from React to plain JS in the following form:
Seems simple enough, right? I asynchronously await the response, so I’ll get all the posts before returning from the function, so the array should be populated.
Life is rarely so simple.
Unfortunately, the browser had other ideas. When I returned the array, it was still blank. When I logged it to the console, it just showed an empty array. For the hell of it I clicked the dropdown beside the log, and was flabbergasted:
The array was populating after it was logged to console, so the values were in the memory location, just not at the time I was trying to access it. For the life of me, despite searching Stack Overflow about lazy array evaluation, I could not figure out why the delay was occurring.
So I did what any reasonable JavaScript dev would have done in my shoes…
The solution
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
while (articles.length == 0) await sleep(250);
I forgot about the why and used a wait function to halt the program until the array was populated.
Was it the most scientific approach? Hell no. Did it return my site to operational status so I could get back to other projects? Yeah. Would I do it again? In a heartbeat.
Do you want to know the best part, the real kicker? The very next day, feed2js was up and running again.
That’s my favorite part of being a developer. When a service goes down, you realize how truly fragile your application is, and it comes out the other side more robust. Now I no longer need to rely on any third party code, it is all written and hosted by me.
Final words
I hope you enjoyed the first real post of The Daily Developer. I plan on releasing content semi-daily, whenever I run into an interesting problem and I can detail the steps I took to resolve it.
Check out all the code links I included, make some PRs to help my work become even more awesome, and go make something amazing of your own!
I would love to hear from some of you about problems of your own that you’ve stumbled across in your travels, and how you ended up overcoming them. Leave a comment or reach out to my email at any time.
If you liked this post and want to see more, hit the subscribe button below. Share it with your developer friends too, let’s get a community going!