<?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: Karem kamal</title>
    <description>The latest articles on DEV Community by Karem kamal (@eskiimo).</description>
    <link>https://dev.to/eskiimo</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%2F292886%2Fc94c8879-c657-4fd2-b48d-c3d7ee86dfe8.jpeg</url>
      <title>DEV Community: Karem kamal</title>
      <link>https://dev.to/eskiimo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eskiimo"/>
    <language>en</language>
    <item>
      <title>Using HTTPS with Callbacks (Non-Promise Approach):</title>
      <dc:creator>Karem kamal</dc:creator>
      <pubDate>Tue, 03 Oct 2023 05:05:50 +0000</pubDate>
      <link>https://dev.to/eskiimo/using-https-with-callbacks-non-promise-approach-5846</link>
      <guid>https://dev.to/eskiimo/using-https-with-callbacks-non-promise-approach-5846</guid>
      <description>&lt;p&gt;&lt;strong&gt;Did&lt;/strong&gt; you know that in Nodejs &lt;code&gt;https.get&lt;/code&gt; does not return a promise ?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;that means warping the request with &lt;code&gt;async/await&lt;/code&gt; won't work as you expect&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WjqIFQC3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np0619qrf77t3capq9sh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WjqIFQC3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/np0619qrf77t3capq9sh.png" alt="async/await approach" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I declared main() as an async function and chained the callback with then .. why does the console.log statement gets called first ???&lt;br&gt;
and if I return the response instead of logging it, that's a &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ReferenceError: Cannot access 'result' before initialization&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  Here's why :
&lt;/h4&gt;

&lt;p&gt;In Node.js, the built-in http module provides methods for making HTTP/HTTPS requests, but it primarily uses a callback-style syntax, which is asynchronous and doesn't return promises.&lt;/p&gt;
&lt;h5&gt;
  
  
  So, while the regular function itself doesn't return a promise, when called using await, its return value behaves as if it were a resolved promise value.
&lt;/h5&gt;
&lt;h4&gt;
  
  
  To make HTTP requests using promises,
&lt;/h4&gt;

&lt;p&gt;you can use the &lt;code&gt;https.request&lt;/code&gt; method in combination with the &lt;strong&gt;Promise constructor&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fsNXcCLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijc7ahgu7ibl5jz6gvxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fsNXcCLB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijc7ahgu7ibl5jz6gvxf.png" alt="Using promise constructor " width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this we make sure the promise has been resolved first.&lt;/p&gt;

&lt;p&gt;Here's another example to make it clearer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function regularFunction() {
  setTimeout(() =&amp;gt; {
    console.log("during await");
  }, 1000);
}

const asyncFunction = async () =&amp;gt; {
  console.log("Before await");
  await regularFunction().then(() =&amp;gt; {
    console.log("After await");
  });
};

asyncFunction();
// before await
// after await
// during await

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;regularFunction&lt;/code&gt; is treated as if it were a resolved promise.&lt;br&gt;
After the &lt;code&gt;await&lt;/code&gt;, the execution of &lt;code&gt;asyncFunction&lt;/code&gt; continues, &lt;code&gt;"After await"&lt;/code&gt; is logged to the console and then &lt;code&gt;"during await"&lt;/code&gt; after the callback execution is done.&lt;/p&gt;

&lt;p&gt;I hope this was helpful. &lt;br&gt;
this is my first post ❤️ . if you liked it give it a 👍&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/35182752/promises-with-http-get-node-js#comment58083725_35182752"&gt;Credits to this comment&lt;/a&gt;&lt;br&gt;
&lt;a href="https://javascript.info/async-await#:~:text=The%20word%20%E2%80%9Casync%E2%80%9D%20before%20a,in%20a%20resolved%20promise%20automatically.&amp;amp;text=So%2C%20async%20ensures%20that%20the,wraps%20non%2Dpromises%20in%20it."&gt;Also check&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>promises</category>
      <category>callbacks</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
