<?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>Forem: Bertil Tandayamo</title>
    <description>The latest articles on Forem by Bertil Tandayamo (@btandayamo).</description>
    <link>https://forem.com/btandayamo</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%2F461554%2F01cabd6c-b7e6-4a86-9c4b-a9c4822a789f.png</url>
      <title>Forem: Bertil Tandayamo</title>
      <link>https://forem.com/btandayamo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/btandayamo"/>
    <language>en</language>
    <item>
      <title>A guide to through async/await with Babel and Webpack</title>
      <dc:creator>Bertil Tandayamo</dc:creator>
      <pubDate>Tue, 01 Sep 2020 13:43:10 +0000</pubDate>
      <link>https://forem.com/btandayamo/a-guide-to-through-async-await-with-babel-and-webpack-mh5</link>
      <guid>https://forem.com/btandayamo/a-guide-to-through-async-await-with-babel-and-webpack-mh5</guid>
      <description>&lt;p&gt;At first, the JavaScript projects with &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack&lt;/a&gt; ran well. Then I realized that our project should be able to run on an outdated browser, so I chose &lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;Babel&lt;/a&gt;. I subsequently &lt;a href="https://medium.com/@imranhsayed/install-and-set-up-babel-for-your-project-a6ce1fc37633" rel="noopener noreferrer"&gt;installed&lt;/a&gt; and ran it.&lt;/p&gt;

&lt;p&gt;The projects include &lt;a href="https://bertil291utn.github.io/restaurant-page/#" rel="noopener noreferrer"&gt;a beautiful restaurant page&lt;/a&gt; and &lt;a href="https://rawcdn.githack.com/NewIncome/mv_todo-list/360b90c602894736e0f744429a057caff0ed5042/index.html" rel="noopener noreferrer"&gt;a cool design to-do list app&lt;/a&gt;. Then APIs came into the scenario. Inevitably I had to deal with &lt;a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;promises&lt;/a&gt;. I can handle them with &lt;code&gt;.then&lt;/code&gt; or &lt;code&gt;async/await&lt;/code&gt; functions.&lt;/p&gt;

&lt;p&gt;I decided to go with &lt;code&gt;async/await&lt;/code&gt;, to write the API function &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;


&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getDataByIpCheck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`http://api.ipstack.com/check?access_key=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;API_KEY_IPSTACK&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;format=1`&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I ran the application &lt;code&gt;npm run start&lt;/code&gt; and 😮, but what's going on, I got an error&lt;/p&gt;

&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%2Fflaviocopes.com%2Fparcel-regeneratorruntime-not-defined%2FScreenshot%25202020-04-05%2520at%252017.45.59.png" 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%2Fflaviocopes.com%2Fparcel-regeneratorruntime-not-defined%2FScreenshot%25202020-04-05%2520at%252017.45.59.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What are we going to do?. I could've cried over our keyboard but I  chose to find a solution.&lt;/p&gt;

&lt;p&gt;This was the solution 💡&lt;/p&gt;




&lt;h2&gt;
  
  
  What &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/polyfill is?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;It's just the import of stable core-js features and regenerator-runtime for generators and async functions, so if you load &lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/polyfill - you load the global version of core-js without ES proposals.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Install babel-polyfill
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm assuming that you've already installed all webpack dependencies, along with babel config inside webpack &lt;a href="https://webpack.js.org/loaders/babel-loader/" rel="noopener noreferrer"&gt;config file&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Through npm add this dependency &lt;code&gt;npm install --save @babel/polyfill&lt;/code&gt; to your project&lt;/p&gt;

&lt;h3&gt;
  
  
  Update webpack file
&lt;/h3&gt;

&lt;p&gt;At the beginning in the module.export object, entry key add &lt;br&gt;
&lt;code&gt;entry: ["@babel/polyfill", "&amp;lt;your enter js file&amp;gt;"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Your file should look like the below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@babel/polyfill&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run again &lt;code&gt;npm run start&lt;/code&gt;&lt;/p&gt;

&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1597788201748%2F3ISCw8FlP.png" 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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1597788201748%2F3ISCw8FlP.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And voilà the project now works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@babel/polyfill&lt;/code&gt; allows us to emulate a full ES2015+ environment, in this case, to work with &lt;strong&gt;async/await&lt;/strong&gt; promise functions. Polyfill adds a global scope to accomplish this &lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;I'd be grateful if you could leave a comment if you found this post helpful or liked it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please follow &lt;a href="https://twitter.com/btandayamo" rel="noopener noreferrer"&gt;@btandayamo&lt;/a&gt; on Twitter&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>babel</category>
      <category>webpack</category>
      <category>es6</category>
    </item>
  </channel>
</rss>
