<?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 Debanshu Mutt on Medium]]></title>
        <description><![CDATA[Stories by Debanshu Mutt on Medium]]></description>
        <link>https://medium.com/@debanshumutt1882000?source=rss-133c5c36d52c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*0NnDbux33zKrTFj8KbAuPA.jpeg</url>
            <title>Stories by Debanshu Mutt on Medium</title>
            <link>https://medium.com/@debanshumutt1882000?source=rss-133c5c36d52c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 13:17:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@debanshumutt1882000/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[Introduction to Promises in JavaScript.]]></title>
            <link>https://medium.com/dsckiit/introduction-to-promises-in-javascript-e02fe711af8c?source=rss-133c5c36d52c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e02fe711af8c</guid>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[async-functions]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Debanshu Mutt]]></dc:creator>
            <pubDate>Fri, 04 Sep 2020 17:01:31 GMT</pubDate>
            <atom:updated>2020-09-08T19:43:49.669Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction to Promises in JavaScript</h3><h3>What are Promises?</h3><p>Promises are a pattern of writing Asynchronous Code.</p><p><em>Promise is an object which represents the eventual success or failure of something which takes time(For Example- An asynchronous operation).</em></p><p><strong>The promise object in JavaScript has 4 states :-</strong></p><blockquote>1.<strong> Fulfilled</strong> : Operation completed.</blockquote><blockquote>2. <strong>Rejected</strong> : Operation Rejected.</blockquote><blockquote>3. <strong>Pending</strong> : Initial State i.e waiting to be fulfilled or rejected.</blockquote><blockquote>4. <strong>Settled</strong>: the promise has been fulfilled or rejected</blockquote><h4>Why is it helpful?</h4><p>Promises are important because when there are multiple asynchronous operations we may find ourselves in a situation called <strong>Callback Hell</strong> and moreover the code becomes difficult to maintain. With Promises, we attach callbacks instead of passing them hence the code looks cleaner and easier to maintain.</p><h4>What is the difference between Callbacks and Promises?</h4><p>We attach callbacks to promises, We don’t pass in callbacks into a function.</p><h3><strong>How to Create a Promise?</strong></h3><p><strong>Promise object can be created using the Promise() constructor.</strong></p><blockquote><em>Promise() constructor takes one argument i.e a callback function. The callback function itself takes two arguments </em><strong><em>resolve</em></strong><em> and </em><strong><em>reject</em></strong><em> which themselves are functions.</em></blockquote><blockquote>If Operation specified in the callback function is successful, we resolve (with or without a value) else we reject.</blockquote><h3>Syntax to create a Promise:-</h3><blockquote>const prom = new Promise((resolve,reject) =&gt;{</blockquote><blockquote>//Some Operation</blockquote><blockquote>});</blockquote><h3><strong>How to Interact with Promises?</strong></h3><p>We learnt how to create a promise and now we are going to understand how to interact with promises, how to run code when a promise is resolved or a promise is rejected.</p><p>Every Promise has a <strong>.then() </strong>method which includes a callback for when the <em>resolve()</em> function executes.</p><p>And a <strong>.catch()</strong> method for when<em> reject()</em> method executes or to handle an error.</p><blockquote><strong>then() : This method is automatically invoked when the promise is resolved.</strong></blockquote><blockquote>Promise.then(()=&gt;{</blockquote><blockquote>//Do something</blockquote><blockquote>});</blockquote><blockquote><strong>.catch() : This method is invoked when the promise is rejected.</strong></blockquote><blockquote>Promise.then(()=&gt;{</blockquote><blockquote>//Do Something</blockquote><blockquote>});</blockquote><h4><strong><em>Example : Lets say if you score above 70 in Mathematics, you get a phone else you have to try again.</em></strong></h4><h3><strong>Code Snippet :-</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*uzY3CYxf6_CCsLq8BuREeQ.png" /><figcaption><strong>Interaction with Promises - Code Snippet</strong></figcaption></figure><h3><strong>Output :-</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*AazGm4UAN5DDQ3GTZqNdbA.png" /><figcaption><strong>Output</strong></figcaption></figure><h3><strong>Explanation :-</strong></h3><p>willYouGetAPhone Promise object is created and it is fulfilled because the condition that Marks in Mathematics should be greater than 70 is successful. Accordingly the resolve() method executes. .then() method automatically runs when a promise is resolved and hence, the Output :<em> Congrats! You get a Phone</em> is printed on the console<em>.</em></p><blockquote>Note : If MathsMarks had been less than 70, .reject() will have executed and accordingly .catch() will run and hence, the Output : <strong>Try Again Next Semester</strong></blockquote><h3><strong>Conclusion :-</strong></h3><p>This was just a brief overview of Promises in JavaScript and stresses on how easily Asynchronous code can be handled with the help of Promises.</p><p>For any doubts and suggestions, you can reach out to me on <a href="https://www.linkedin.com/in/debanshu-mutt-9045821a2/">LinkedIn </a>.</p><p>If you like the article, Do give a 👏.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e02fe711af8c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsckiit/introduction-to-promises-in-javascript-e02fe711af8c">Introduction to Promises in JavaScript.</a> was originally published in <a href="https://medium.com/dsckiit">GDG KIIT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>