<?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: Sambit Kumar Nanda👾</title>
    <description>The latest articles on DEV Community by Sambit Kumar Nanda👾 (@sambit20).</description>
    <link>https://dev.to/sambit20</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%2F639669%2F5ae1cd50-350f-48f2-a0fd-037584b8d536.jpg</url>
      <title>DEV Community: Sambit Kumar Nanda👾</title>
      <link>https://dev.to/sambit20</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sambit20"/>
    <language>en</language>
    <item>
      <title>What is a promise in JavaScript ?</title>
      <dc:creator>Sambit Kumar Nanda👾</dc:creator>
      <pubDate>Sat, 06 Aug 2022 15:44:02 +0000</pubDate>
      <link>https://dev.to/sambit20/what-is-a-promise-in-javascript--1nkm</link>
      <guid>https://dev.to/sambit20/what-is-a-promise-in-javascript--1nkm</guid>
      <description>&lt;p&gt;Promises are used to handle asynchronous tasks in JavaScript. Managing them is easier when dealing with multiple asynchronous operations, where we’ll get stuck in callback hell, created by callbacks which ultimately leads to unmanageable code. A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. A promise is always in one of these states:&lt;/p&gt;

&lt;p&gt;● Pending&lt;br&gt;
● Resolved&lt;br&gt;
● Rejected&lt;br&gt;
Creating Promises&lt;br&gt;
We can create a promise using the promise constructor:&lt;br&gt;
&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;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;reject&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="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&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;Promise constructor takes a function as a single argument, which takes two arguments, resolve and reject. We will call resolve if everything inside the function goes well, else we call reject. A pending promise can be resolved by providing a value or rejected by providing a reason (error). If any of these options occur, we must take the appropriate steps. The methods promise.then() and promise.catch() are used to take any further action with a promise that becomes settled.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;then():&lt;/strong&gt;&lt;br&gt;
When a promise is resolved or rejected, then() is called. Two functions are passed to the then() method. If the promise is resolved and a result is received, the first function is called. If the promise is rejected and an error is returned, the second function is called. (It's optional as the catch() method is comparatively a better way to handle errors.)&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&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;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;data&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="c1"&gt;// On Resolved&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="c1"&gt;// On Rejected&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;catch():&lt;/strong&gt;&lt;br&gt;
catch() is called to handle the errors, i.e., when a promise is rejected or when some error has occurred during the execution. catch() method takes one function as an argument, which is used to handle errors.&lt;br&gt;
Example:&lt;br&gt;
&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;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="c1"&gt;// Handle Error on Rejected or caught Error&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is an Api ?</title>
      <dc:creator>Sambit Kumar Nanda👾</dc:creator>
      <pubDate>Sat, 29 May 2021 03:10:58 +0000</pubDate>
      <link>https://dev.to/sambit20/what-is-an-api-42j7</link>
      <guid>https://dev.to/sambit20/what-is-an-api-42j7</guid>
      <description>&lt;h1&gt;
  
  
  Application Programming Interface
&lt;/h1&gt;

&lt;p&gt;Think of it as a set of tools designed for software developers. API makes your life easier by giving you access to data and providing you with an abstraction of the implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interface
&lt;/h2&gt;

&lt;p&gt;-&amp;gt; Interface, through which the user interacts. The developer knows what' s happening behind the scenes.&lt;br&gt;
-&amp;gt; When the user clicks, it does something! APIs works actually as interfaces that are abstracting details for him.&lt;/p&gt;

&lt;h2&gt;
  
  
  Flashlight API
&lt;/h2&gt;

&lt;p&gt;-&amp;gt; Let’s say when the user clicks, it throws the flashlight. So, it' s worth noting the code probably also uses the Flashlight API&lt;br&gt;
-&amp;gt; This API abstracts away the details of how to send flashlight to the user when he interacts with the button&lt;br&gt;
-&amp;gt; You just need to call on/off method of a Flashlight API to make it work!&lt;/p&gt;

&lt;h2&gt;
  
  
  UI V/S API
&lt;/h2&gt;

&lt;p&gt;-&amp;gt; User Interface (UI) is made for the user interaction.&lt;br&gt;
-&amp;gt; Application Programming Interface (API) is made for the programmers to use. They save you   from needing to create everything yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  SOAP APIs V/S REST APIs
&lt;/h2&gt;

&lt;p&gt;-&amp;gt; Soap/Web based API calls are sent from web apps using XML between systems through HTTP&lt;br&gt;
-&amp;gt; Rest is a style of architecture that provides standards on the web between systems&lt;br&gt;
-&amp;gt; Well, SOAP stands for Service Object Access Protocol and REST stands for Representational State Transfer&lt;br&gt;
-&amp;gt; Soap/Web API can only work with XML but Rest API can work with plain text, HTML, XML, JSON, etc.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
