<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jamie Halvorson</title>
    <description>The latest articles on DEV Community by Jamie Halvorson (@jhalvorson).</description>
    <link>https://dev.to/jhalvorson</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F65367%2F46471ccf-4377-4665-ba4a-5e198317125c.JPG</url>
      <title>DEV Community: Jamie Halvorson</title>
      <link>https://dev.to/jhalvorson</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhalvorson"/>
    <language>en</language>
    <item>
      <title>How do I use Async/Await with Array.map?</title>
      <dc:creator>Jamie Halvorson</dc:creator>
      <pubDate>Thu, 18 Apr 2019 14:02:41 +0000</pubDate>
      <link>https://dev.to/jhalvorson/how-do-i-use-async-await-with-array-map-1h3f</link>
      <guid>https://dev.to/jhalvorson/how-do-i-use-async-await-with-array-map-1h3f</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe6dpui3dg7zcxxobdr3x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fe6dpui3dg7zcxxobdr3x.jpg" alt="Await"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can't, but you can do something similar. This always trips me up so I thought I'd better write it down.&lt;/p&gt;

&lt;p&gt;You can't async/await &lt;code&gt;Array.map&lt;/code&gt;  since &lt;strong&gt;synchronous code won't sit and wait for your asynchronous code to resolve&lt;/strong&gt;, instead it will the fire the function and move on. This is desired behaviour as we don't want our &lt;code&gt;Array.map&lt;/code&gt; to block the thread otherwise nothing would run whilst &lt;code&gt;Array.map&lt;/code&gt; goes and does its thing. Knowing that is cool, however, it doesn't really help us if we need to await our &lt;code&gt;Array.map()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thankfully there is a solution, we can achieve what we want by utilising &lt;code&gt;Promise.all&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example, the following won't work:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const postIds = ['123', 'dn28e29', 'dn22je3'];

const posts = await postIds.map(id =&amp;gt; {
  return axios
    .get(`https://example.com/post/${id}`)
    .then(res =&amp;gt; res.data)
    .catch(e =&amp;gt; console.error(e));
}

console.log(posts) // [] it returns the promise, not the results 💩
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But this will:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const postIds = ['123', 'dn28e29', 'dn22je3'];

const posts = posts.map(post =&amp;gt; {
  return axios
    .get(`https://example.com/post/${post.id}`)
    .then(res =&amp;gt; res.data)
    .catch(e =&amp;gt; console.error(e));
}

Promise.all(posts).then(res =&amp;gt; console.log(`We have posts: ${res}!`));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Instead of immediately returning the &lt;code&gt;Array.map&lt;/code&gt; we're assigning it to a variable and passing that variable to &lt;code&gt;Promise.all&lt;/code&gt;, once that resolves we then have access to the new array returned by &lt;code&gt;Array.map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Happy JavaScripting.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
