<?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 Shuning H on Medium]]></title>
        <description><![CDATA[Stories by Shuning H on Medium]]></description>
        <link>https://medium.com/@shuningch?source=rss-c9ad21dce0ec------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*PW6ZnGqU-gw50c3a</url>
            <title>Stories by Shuning H on Medium</title>
            <link>https://medium.com/@shuningch?source=rss-c9ad21dce0ec------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 05:05:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shuningch/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[async/await diet]]></title>
            <link>https://shuningch.medium.com/async-await-diet-2d06883bdba3?source=rss-c9ad21dce0ec------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d06883bdba3</guid>
            <category><![CDATA[coding-style]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <category><![CDATA[asyncawait]]></category>
            <dc:creator><![CDATA[Shuning H]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 06:43:21 GMT</pubDate>
            <atom:updated>2021-02-03T06:43:21.020Z</atom:updated>
            <content:encoded><![CDATA[<p>await/async comes with ES6 since 2015, right before I got my CS degree and started my first job as a web developer.</p><p>It’s been almost 6 years, I finally decide, it is time to reflect how and why I am overdosing await/async.</p><p>Take a look at the code I abstracted from the real world.</p><pre>const asyncFunc = async (n: number): Promise&lt;void&gt; =&gt; {<br>  console.log(&#39;entering asyncFunc: &#39;, n)<br>  await new Promise(res =&gt; {<br>    setTimeout(_ =&gt; {<br>      console.log(&#39;leaving asyncFunc: &#39;, n)<br>      res(n)<br>    })<br>  })<br>}</pre><pre>(async () =&gt; {<br>  const arr = Array(5).fill(1)</pre><pre>  console.log(&#39;async/await overdose&#39;)<br>  const oldCode = await Promise.all(<br>    arr.map(async (_, i: number) =&gt; await asyncFunc(i))<br>  )<br>  console.log(oldCode, &#39;\n----------&#39;)<br>})()</pre><p>This code is expected to run several asynchronous calls in parallel and catch all resolved results. There is definitely nothing wrong with this code, it passes all tests, what is more, I feel so secure when there is await/async everywhere.</p><p>When I looked back at this code, I asked myself, do I really need that many await/async?</p><pre>// async/await removed<br>const asyncFunc = (n: number): Promise&lt;number&gt; =&gt; {<br>  console.log(&#39;entering asyncFunc: &#39;, n)<br>  return new Promise(res =&gt; {<br>    setTimeout(_ =&gt; {<br>      console.log(&#39;leaving asyncFunc: &#39;, n)<br>      res(n)<br>    })<br>  })<br>}</pre><pre>(async () =&gt; {<br>  const arr = Array(5).fill(1)</pre><pre>  console.log(&#39;async/await&#39;)<br>  const remRedundancy = await Promise.all(<br>    arr.map((_, i: number) =&gt; asyncFunc(i)) // async/await removed<br>  )<br>  console.log(remRedundancy, &#39;\n----------&#39;)<br>})()</pre><p>They work exactly the same. It is not a big deal using await/async wherever you can. And if you are writing Javascript, you can even argue that putting async before every asynchronous function helps reviewer and other developers understanding its behavior at a glance.</p><p>To be honest, I have been putting it everywhere just because I was not confident about how Promise.all, Array.map, async/await work. After I got a better idea of those methods and syntax, I decided to go on a diet.</p><p>Let’s take a look at what does <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all">Promise.all</a> do.</p><blockquote>The <strong>Promise.all()</strong> method takes an iterable of promises as an input, and returns a single <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input&#39;s promises have resolved</blockquote><p>Alright, so have no problem with putting await before Promise.all in order to get the resolved result.</p><p>Then how about <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map">Array.map</a> here.</p><blockquote>The <strong>map()</strong> method <strong>creates a new array</strong> populated with the results of calling a provided function on every element in the calling array.</blockquote><p>We can look into Array.map’s <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#polyfill">polyfill</a> to see how this provided function(callback) gets called.</p><pre>mappedValue = callback.call(T, kValue, k, O);</pre><p>No matter your callback is asynchronous or not, it gets called synchronously, it does not promise to wait for anything, which makes the await in our first snippet useless.</p><pre>arr.map(async (_, i: number) =&gt; await asyncFunc(i))</pre><p>Because according to the definition of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await">await</a> in MDN.</p><blockquote>The await operator is used to wait for a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>. It can only be used inside an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function">async function</a>.</blockquote><p>As long as asyncFunc returns a Promise, removing await/async from it in our example is absolutely harmless.</p><p>Overdose of async/await probably means nothing to a production, however it definitely helps me figuring out what I am lack in and it’s been really fun.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d06883bdba3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>