<?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: Akanksha </title>
    <description>The latest articles on DEV Community by Akanksha  (@akanksha108).</description>
    <link>https://dev.to/akanksha108</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%2F681658%2F84251ff9-d59c-40c0-9772-31c4732d938e.png</url>
      <title>DEV Community: Akanksha </title>
      <link>https://dev.to/akanksha108</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akanksha108"/>
    <language>en</language>
    <item>
      <title>Illegal Use of Break Statement Error in JavaScript</title>
      <dc:creator>Akanksha </dc:creator>
      <pubDate>Mon, 09 Aug 2021 09:40:04 +0000</pubDate>
      <link>https://dev.to/akanksha108/illegal-use-of-break-statement-error-in-javascript-1jp5</link>
      <guid>https://dev.to/akanksha108/illegal-use-of-break-statement-error-in-javascript-1jp5</guid>
      <description>&lt;p&gt;Have you wondered, why we get &lt;code&gt;Illegal use of break statement error&lt;/code&gt; in JavaScript? &lt;br&gt;
While using loops in JavaScript, you might have got stuck because of this error. In this blog, we will discuss why we get this error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Loops&lt;/strong&gt;&lt;br&gt;
A loop is a sequence of instructions that is continuously repeated until a certain condition is reached. They are used if we want to perform a task 'n' number of times.&lt;br&gt;
There are different kinds of loops we can use, such as for loop, while loop, do-while loop, forEach loop, etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Break Statement&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;break&lt;/code&gt; statement is a loop control statement that is used to terminate the loop. As soon as the &lt;code&gt;break&lt;/code&gt; statement is encountered, the loop is terminated and the control comes out of the loop, to execute the immediately next statement after the loop.&lt;br&gt;
                    &lt;strong&gt;&lt;code&gt;Syntax: break;&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, lets see when do we get &lt;code&gt;Illegal use of break statement&lt;/code&gt; error. Lets take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(year2 &amp;gt; 1){
            date2 = date2 - 1;
            if(date2 &amp;lt; 1){
                month2 = month2 - 1;        
                if(month2 &amp;lt; 1){
                    month2 = 12;
                    year2 = year2 - 1;      
                    if(year2 &amp;lt; 1){
                        break;
                    }
                    date2 = datesInMonth[month2 - 1];
                }
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the above example, we will get the error &lt;code&gt;Illegal use of break statement&lt;/code&gt;. That is because, break statement is used to break out of a loop like for, while, do-while, etc. But here, we do not have a loop, we are using &lt;code&gt;if&lt;/code&gt; statement, which is a conditional statement.&lt;/p&gt;

&lt;p&gt;So, in this case, we need to use &lt;code&gt;return&lt;/code&gt; statement to break the execution flow of the current function and return to the calling function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(year2 &amp;gt; 1){
            date2 = date2 - 1;
            if(date2 &amp;lt; 1){
                month2 = month2 - 1;        
                if(month2 &amp;lt; 1){
                    month2 = 12;
                    year2 = year2 - 1;      
                    if(year2 &amp;lt; 1){
                       return;
                    }
                    date2 = datesInMonth[month2 - 1];
                }
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the error is solved and our program will execute properly.&lt;/p&gt;

&lt;p&gt;I hope you found this helpful!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>errors</category>
      <category>breakstatement</category>
    </item>
    <item>
      <title>Promises in JavaScript</title>
      <dc:creator>Akanksha </dc:creator>
      <pubDate>Mon, 09 Aug 2021 06:00:33 +0000</pubDate>
      <link>https://dev.to/akanksha108/promises-in-javascript-1n5h</link>
      <guid>https://dev.to/akanksha108/promises-in-javascript-1n5h</guid>
      <description>&lt;p&gt;Hello, everyone!!!&lt;br&gt;
This is my first blog and this blog will give you basic understanding of how promises work in JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is JavaScript?&lt;/strong&gt;&lt;br&gt;
JavaScript often abbreviated as JS, is the Programming Language for Web. It is a single-threaded language, meaning it can do only one task at a time. It cannot run two pieces of code simultaneously. When one piece of code gets complete, the piece can start executing/running.&lt;/p&gt;

&lt;p&gt;Previously, before promises were introduced, callbacks were used to handle asynchronous operations. But for multiple asynchronous operations, callbacks can create callback hell leading to unmanageable code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is a Promise?&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/em&gt; - MDN Web Docs&lt;/p&gt;

&lt;p&gt;A Promise in JavaScript is just like a promise in real-world. Lets take an example:- Your friend promised to get you a chocolate, when he/she meets you. Now, your friend has just made a promise, you don't know whether he/she will really get you a chocolate or not. So, we can say that the promise is pending. If he/she gets you a chocolate, then promise is completed. If he/she doesn't get you a chocolate, it means that promise is rejected.&lt;/p&gt;

&lt;p&gt;Similarly, in JavaScript, a Promise is an object that holds the future value of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;A Promise has 4 States:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Fulfilled or Resolved - Action related to Promise succeeded. It &lt;br&gt;
means that the promise is completed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rejected or Unresolved - Action related to Promise failed. It &lt;br&gt;
means that the Promise is not completed due to some error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Pending - It means that the Promise is neither fulfilled nor &lt;br&gt;
rejected.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Settled - It means that Promise has been fulfilled or rejected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M6sBZ7s3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8yf9d9ewmzbvlllfoxz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M6sBZ7s3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8yf9d9ewmzbvlllfoxz.png" alt="JS Promises"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Promise&lt;/strong&gt;&lt;br&gt;
A Promise object can be created using its constructor and &lt;em&gt;new&lt;/em&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var myPromise = new Promise(function(resolve, reject) =&amp;gt; {
        const x = 2;
        const y = 2;

     if(x == y){
        resolve();
     }else{
        reject();
     }
});

myPromise
  .then(function(){
      console.log("Success");
   })
   .catch(function(){
       console.log("Error");
   });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
Success
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Promise constructor takes only one argument, a callback function which is also known as executor function. The callback function takes 2 arguments: resolve and reject.&lt;br&gt;
In the above example, if the value of x is equal to the value of y, then promise is resolved. If value of x is not equal to y, then promise is rejected.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Consuming a Promise&lt;/strong&gt;&lt;br&gt;
Now that we know how to create a promise, lets understand how to consume a promise. We consume a promise by calling then() and catch() methods on the promise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.then()&lt;/strong&gt; - This method is invoked when a promise is resolved or rejected. It takes two parameters.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.then() Syntax : promise.then(successCallback, failureCallback)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The first function that is &lt;code&gt;successCallback&lt;/code&gt; is executed if promise is resolved and result is received.&lt;br&gt;
The second function that is &lt;code&gt;failureCallback&lt;/code&gt; is executed if promise is rejected and error is received. This is optional. It is always better to use &lt;code&gt;.catch()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.catch()&lt;/strong&gt; - This method is invoked when promise is rejected or some error has occurred in execution. This method is used for handling errors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.finally()&lt;/strong&gt; - When is promise is settled(i.e. either resolved or rejected), the callback function in finally() method is invoked. This method is called regardless of whether our promise was rejected or resolved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var promise = new Promise((resolve, reject) =&amp;gt; {
          resolve("Promise resolved");
});

promise 
   .then((data) =&amp;gt; {
      console.log(data);
})
.catch((error) =&amp;gt; {
   console.log("error");
})
.finally(() =&amp;gt; {
   console.log("Finally block is executed");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Output:
Promise Resolved
Finally block is executed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
We learnt what are promises and how to use them. We also saw how to create promises and consume them.&lt;/p&gt;

&lt;p&gt;I hope you found this blog helpful!!!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>asynchronousoperation</category>
    </item>
  </channel>
</rss>
