<?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: Syeda Umme Kulsoom</title>
    <description>The latest articles on DEV Community by Syeda Umme Kulsoom (@ummekulsoomshah).</description>
    <link>https://dev.to/ummekulsoomshah</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%2F1176485%2Fc4c7f3b3-f335-4f7e-ba51-5c0bbbb8c492.PNG</url>
      <title>DEV Community: Syeda Umme Kulsoom</title>
      <link>https://dev.to/ummekulsoomshah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ummekulsoomshah"/>
    <language>en</language>
    <item>
      <title>Interview Questions About Promises</title>
      <dc:creator>Syeda Umme Kulsoom</dc:creator>
      <pubDate>Sun, 11 Feb 2024 06:55:37 +0000</pubDate>
      <link>https://dev.to/ummekulsoomshah/interview-questions-about-promises-5b</link>
      <guid>https://dev.to/ummekulsoomshah/interview-questions-about-promises-5b</guid>
      <description>&lt;p&gt;~~~~## Q1&lt;br&gt;
&lt;strong&gt;What are Promises?&lt;/strong&gt;&lt;br&gt;
In the world of JavaScript, promises are your go-to solution for handling asynchronous operations. But what exactly are promises? Well, think of them as placeholders for values that are not yet known. They represent the eventual completion or failure of an asynchronous operation and allow you to handle its result asynchronously.&lt;/p&gt;

&lt;p&gt;Imagine you're waiting for a package delivery. You don't know exactly when it will arrive, but you're certain it will eventually either show up at your doorstep or encounter some delivery issues. Promises work similarly—they represent the future outcome of an operation, be it successful or unsuccessful.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;States of Promises&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pending&lt;/li&gt;
&lt;li&gt;Resolved&lt;/li&gt;
&lt;li&gt;Rejected&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Q2:Sleep Function
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Synatx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sleep(ms) {
  return new Promise(resolve =&amp;gt; setTimeout(resolve, ms))

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Solution To Callback hell&lt;/strong&gt;&lt;br&gt;
_In order to convert callback based function into promise based  wrap the callback fucntion into promise and [ass] the handlers resolve and reject ,and you can call the resolve function after successfull completion of the asynchronouse operation _&lt;/p&gt;

&lt;p&gt;Lets take an example&lt;/p&gt;
&lt;h2&gt;
  
  
  Q3
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Callback Function&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getdata(dataid,getnextdata){
  setTimeout(()=&amp;gt;{
    console.log("data",dataid)
    if(getnextdata){
      getnextdata()}

  },2000)
}

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

&lt;/div&gt;



&lt;p&gt;Promise Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getdata(dataid, getnextdat) {
  return new Promise((resolve, reject) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      console.log("data", dataid)
      resolve("success")
    }, 2000)
    if (getnextdat) {
      getnextdat()
    }

  })

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Q4
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Promise Chaining&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fetchPromise = fetch(
  "https://mdn.github.io/learning-area/javascript/apis/fetching-data/can-store/products.json",
);

fetchPromise.then((response) =&amp;gt; {
  const jsonPromise = response.json();
  jsonPromise.then((data) =&amp;gt; {
    console.log(data[0].name);
  });
});

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Code By MDN WEB DOCS&lt;/em&gt;&lt;u&gt;[How to use Promises]&lt;/u&gt;(&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise&lt;/a&gt;)&lt;/p&gt;




&lt;p&gt;That's It&lt;br&gt;
fOLLOW ME for more &lt;u&gt;[LINKEDIN ACCOUNT]&lt;/u&gt;(&lt;a href="https://www.linkedin.com/in/syeda-umm-e-kulsoom-259001268/"&gt;https://www.linkedin.com/in/syeda-umm-e-kulsoom-259001268/&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
