<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by David Okolo on Medium]]></title>
        <description><![CDATA[Stories by David Okolo on Medium]]></description>
        <link>https://medium.com/@david.okolo?source=rss-1fa8d459c8f5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZUUWGCdvzbctIJPqgtYZIQ@2x.jpeg</url>
            <title>Stories by David Okolo on Medium</title>
            <link>https://medium.com/@david.okolo?source=rss-1fa8d459c8f5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:09:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@david.okolo/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using async/await in a forEach loop (you can’t)]]></title>
            <link>https://blog.devgenius.io/using-async-await-in-a-foreach-loop-you-cant-c174b31999bd?source=rss-1fa8d459c8f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/c174b31999bd</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[arrays]]></category>
            <category><![CDATA[optimization]]></category>
            <dc:creator><![CDATA[David Okolo]]></dc:creator>
            <pubDate>Wed, 19 Jan 2022 10:01:33 GMT</pubDate>
            <atom:updated>2022-01-24T22:10:12.007Z</atom:updated>
            <content:encoded><![CDATA[<p>I came across a complaint on how the forEach loop is inconsistent when you pass in an asynchronous lambda function as the callback argument to be run for every element. On the surface, it doesn’t seem like an unrealistic expectation but in this article, I will try to explain why it doesn’t work, show you a popular way developers implement this, and finally an optimization that works for a specific scenario (this is usually the scenario most people need this for).</p><h3>Quick background (async/await)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*I5lcJYyRfqDNagmexbDNqA.png" /></figure><p>If you already have a firm grasp on async/await you can skip this section. Something else I have noticed is, kinda like me most people do not understand <a href="https://www.freecodecamp.org/news/javascript-promises-explained/#:~:text=What%20is%20a%20promise%20in,operation%2C%20and%20its%20resulting%20value.">javascript promises</a> and because of that, they do not understand what the async-await syntax is actually doing. The async-await syntax is just syntactic sugar on top of the promises API, the async tag on a function simply lets javascript know that this function would return a promise and the awaits inside the functions tell the interpreter to stay on this line of code inside this function call till the promise called on that line is fully resolved. It makes the ugly nested .then() calls look a lot more readable.</p><h3>The problem (Promises)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*Mahu7lj86a4IZ9KI7uG3Yg.png" /></figure><p>The forEach loop was not built to work with asynchronous callback functions which is the reason it does not do what you may be expecting. It does not wait for the promise of an iteration to be resolved before it goes on to the next iteration. This means that by the end of the forEach loop’s iterations nothing has actually been pushed into the users’ array (it gets pushed a little later but I do not really want to get into the <a href="https://www.digitalocean.com/community/tutorials/understanding-the-event-loop-callbacks-promises-and-async-await-in-javascript">event loop</a> here).</p><h3>The Popular Solution (our good old for loop)</h3><p>Looking closely at the problem you would see that the main reason our code does not work correctly is that we are trying to use the forEach loop for something it was not created for. We need something that lets us iterate over an array and allows us to block the program execution till our promise is resolved and that is where the base for loop comes in.</p><p>We could use either the for…of the loop or the old for(let i = 0;….) type of for loop but I will use for…of here because we don’t need that much control over the iteration process.</p><p>Nore: The for…of the loop has to be inside an async function to use await inside the loop. This syntax cannot be used at the top level of your program unless you have set some extra config on your running environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*U3QcBq-3S68-Dvj_pN0pwg.png" /></figure><p>This would work exactly as you expect and the resolved users would be logged.</p><h3>Optimization (we can do a little better can’t we)</h3><p>Looking at our solution from the previous section, you can see some problems that can arise. Imagine it takes 5 seconds to get the user on the getUser call, that would mean it would take 25 seconds to log all the users and this is even worse when you look at this problem at a larger scale.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ot1oZuX3HikONxIyAotVWA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*VN0sIxpu_Z3gYulmkIzKUQ.png" /></figure><p>For this particular scenario of getting users from an array of user ids, you may notice that we are waiting for one user to be returned before we begin fetching the next user but we do not need to wait because fetching the next user has no dependency on the value returned from the previous call. Can we fetch them all in parallel so it’ll take 5 seconds to get them all instead? yeah, definitely. This is where Promise.all() comes in, we use it to batch the resolution of the promises.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AXkoVJs1GMdWzLbD77oCow.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*P2WccozUqn9pqRAexLlsVg.png" /></figure><p>Using Promise.all(), all promises are being resolved in parallel it’ll take the same time it takes to resolve one to resolve all of them. One important thing to note is that you get access to all resolved promises only when all the promises are resolved so if one of the calls takes 10 seconds while the rest takes 2 seconds, you will have to wait 10 seconds.</p><h3>Conclusion</h3><p>I’m not really a writer so I hope you understood this and I’m done. Also, if you have any better ways to do this please let me know in the comments, I like learning new things too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c174b31999bd" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/using-async-await-in-a-foreach-loop-you-cant-c174b31999bd">Using async/await in a forEach loop (you can’t)</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Journey: A Self-Taught Software Engineer]]></title>
            <link>https://medium.com/@david.okolo/my-journey-a-self-taught-software-engineer-e2da74580ab9?source=rss-1fa8d459c8f5------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2da74580ab9</guid>
            <category><![CDATA[learning-to-code]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[David Okolo]]></dc:creator>
            <pubDate>Sun, 28 Jul 2019 08:58:24 GMT</pubDate>
            <atom:updated>2019-07-28T08:58:24.115Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*c4yVwQua5p1SnJ2qj1YnhA@2x.jpeg" /></figure><p>There are a few things I regret on my relatively young journey as a software engineer and not starting with an intro to CS (computer science) course or actually studying CS in University is one of them. Almost anyone can learn to write code but what sets you apart is your use of CS principles and computational thinking to solve problems as efficiently as possible. How you learn them (formally or self-taught) isn’t all that important but you need to learn them.</p><p>I started my journey about 5 years ago with C++ in the only programming course I was exposed to as a 300 level Electrical and Electronics Engineering undergrad at Covenant University, Nigeria. After that course, I moved into web development learning JavaScript, CSS and HTML, I had always been a curious person so that led me to learning server side programming using PHP and MySQL. To keep up with the tech community I learnt frameworks and assembled a stack I used primarily. At that point, I felt I had my primary stack MongoDB, Express, Angular and Nodejs which I used to create web apps and I felt it was time to apply for a job.</p><h3>My First Job Interview</h3><p>I got approached by a company and was sent a coding challenge to complete in 48 hrs. I was expecting to see a question like “Create a RESTful API……..?” But to my surprise I saw a question on the binary tree data structure which I had no idea about at that point, I decided let’s skip that one.</p><p>The second question was on writing an algorithm (sorting, searching), I like to think I’m very smart so I thought about a way to solve that problem and wrote code that passed all their tests in some hours (I felt like a beast 💪🏽). The third question was to actually create a part of an application using ES5 OOP which I knew so I ran it quickly and submitted the challenge.</p><p>After a few days, I got an online interview and Iogged on feeling like a fresh hot bread someone wanted to buy. The interviewer (a senior engineer at the company) sat with me to go over my code challenge submission. Without even seeing my CV he said “You’re self-taught Yeah?” I replied and we began. He started with my OOP project and told me I didn’t adhere to any of the SOLID design principles (I didn’t even know what that was), things got real to me fast. Next, he went to my algorithm and said the time complexity was exponential but could easily be “log n” (what the hell was a time complexity?). Now, he got to why I didn’t touch the binary tree and I said “I use JavaScript but binary trees are written in Java and C” (I did some flimsy search on the internet and saw most of the examples were in Java using the keyword “Class” so I concluded it wasn’t in JavaScript) he laughed and ended the interview. I was broken.</p><h3>My Rebirth</h3><p>I picked myself up after a few days of doubting I knew anything and started research on the things he talked about. That’s when I watched Professor Eric Grimson’s MIT 6.0001 lecture on Program Efficiency. I finally found time complexity and the big-O notation, I started the course from the beginning and learnt python on the way while solving their problem sets. From that alone, I learnt Recursion (Towers of Hanoi etc), Searching and Sorting Algorithms (Merge, Bubble, Bisection etc) and understanding efficiency. I found out some of what I was missing so I pressed on and went through Harvard’s CS50 with the wonderful David Malan who explained everything so well. I added the understanding of how code I write is compiled to machine code, Data Structures and picked up C programming in the process.</p><p>That was how I was introduced to what was taught to Computer Science students during their time in University so I went further on my own to learn more. This led me to the realization that if you understand how a programming language compiles your code and it’s syntax then learning a new programming language isn’t a difficult task at all and learning multiple languages to write the same subpar and inefficient code isn’t making you better.</p><p>You might think your program still works how you write it and that’s because computers have gotten faster but trust me those milliseconds add up when you start dealing with the rapidly growing size of data.</p><h3>Final Words</h3><p>If I could go back to the beginning, what I’ll start with is understanding CS principles and the ability to think computationally before learning any language.</p><p>If you’re about to enter the university and being a software engineer is your dream please study Computer Science, don’t let anyone tell you “The field is too narrow, study Engineering then you’ll divert to computer science in Masters” it’s a lie you might regret. If you’re already past that and already developing applications please learn CS principles everything would become a lot clearer</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2da74580ab9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>