<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Promises Archives - scmGalaxy</title>
	<atom:link href="https://www.scmgalaxy.com/tutorials/tag/promises/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.scmgalaxy.com/tutorials/tag/promises/</link>
	<description>A Platform for Software Engineers</description>
	<lastBuildDate>Wed, 13 Apr 2022 16:30:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1</generator>
	<item>
		<title>How does the JavaScript Promise work?</title>
		<link>https://www.scmgalaxy.com/tutorials/how-does-the-javascript-promise-work/</link>
					<comments>https://www.scmgalaxy.com/tutorials/how-does-the-javascript-promise-work/#respond</comments>
		
		<dc:creator><![CDATA[rajeshkumar]]></dc:creator>
		<pubDate>Tue, 17 Aug 2021 12:45:21 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[Operation]]></category>
		<category><![CDATA[Promises]]></category>
		<guid isPermaLink="false">https://www.scmgalaxy.com/tutorials/?p=8048</guid>

					<description><![CDATA[<p>Promise A Promise is an object representing the eventual completion or failure of an asynchronous operation. A JavaScript Promise object [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/how-does-the-javascript-promise-work/">How does the JavaScript Promise work?</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading"><strong>Promise</strong></h2>



<p class="wp-block-paragraph">A Promise is an object representing the eventual completion or failure of an asynchronous operation. A JavaScript Promise object contains both the producing code and calls to the consuming code. It can be used to deal with Asynchronous operations in JavaScript.</p>



<p class="wp-block-paragraph"><strong>Promise State:-</strong></p>



<p class="wp-block-paragraph">Pending &#8211; Initial State, Not yet Fulfilled or Rejected<br>Fulfilled/Resolved – Promise Completed<br>Rejected – Promise Failed</p>



<h2 class="wp-block-heading"><strong>How Promise works</strong></h2>



<figure class="wp-block-image size-full is-resized"><img fetchpriority="high" decoding="async" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/08/how-promise-works.png" alt="" class="wp-image-8050" width="827" height="402" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/08/how-promise-works.png 553w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/08/how-promise-works-300x146.png 300w" sizes="(max-width: 827px) 100vw, 827px" /></figure>



<ul class="wp-block-list"><li>A pending promise can either be Resolved with a value or Rejected with a reason (error).</li><li>When either of these options happens, the associated handlers queued up by a promise&#8217;s then method are called.</li><li>A promise is said to be settled if it is either Resolved or Rejected, but not Pending.</li></ul>



<h2 class="wp-block-heading"><strong>Creating Promise</strong></h2>



<p class="wp-block-paragraph">Promise () – A Promise object is created using the new keyword and its constructor. This constructor takes a function, called the &#8220;executor function&#8221;, as its parameter. This function should take two functions as parameters. The first of these functions (resolve) is called when the asynchronous task completes successfully and returns the results of the task as a value. The second (reject) is called when the task fails and returns the reason for failure, which is typically an error object.</p>



<p class="wp-block-paragraph">Syntax:- Promise (executor)</p>



<script src="https://gist.github.com/vijaypk376/3e7af5aa79a5d7a87e3f2a9a5c422046.js"></script>



<p class="wp-block-paragraph">A JavaScript Promise object contains both the producing code and calls to the consuming code.</p>



<h2 class="wp-block-heading"><strong>Function Returning a Promise</strong></h2>



<script src="https://gist.github.com/vijaypk376/3a1622149d11b5cb7992e09699cfcab4.js"></script>



<h2 class="wp-block-heading"><strong>then( ) Method</strong></h2>



<p class="wp-block-paragraph">The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure cases of the Promise. As then the method returns a Promise so we can do method chaining.</p>



<p class="wp-block-paragraph">Syntax:- then(onResolved, onRejected)</p>



<p class="wp-block-paragraph">onResolved &#8211; A Function called if the Promise is fulfilled. This function has one argument, the fulfillment value.</p>



<p class="wp-block-paragraph">onRejected &#8211; A Function called if the Promise is rejected. This function has one argument, the rejection reason.</p>



<script src="https://gist.github.com/vijaypk376/5f3dff1255544e534673c129d0a72fed.js"></script>



<h2 class="wp-block-heading"><strong>Promise</strong></h2>



<script src="https://gist.github.com/vijaypk376/51a3a49dd2cfe1be5fc0cf437b652753.js"></script>



<script src="https://gist.github.com/vijaypk376/1b7e2b3d633315899cc0017a944b974b.js"></script>



<h2 class="wp-block-heading"><strong>Chaining</strong></h2>



<p class="wp-block-paragraph">The then method returns a Promise which allows for method chaining. If the function passed as a handler to then returns a Promise, an equivalent Promise will be exposed to the subsequent then in the method chain.</p>



<script src="https://gist.github.com/vijaypk376/8ea687e432c326ee1092e97c37eb91b2.js"></script>



<h2 class="wp-block-heading"><strong>catch () Method</strong></h2>



<p class="wp-block-paragraph">The <em>catch()</em> method returns a Promise and deals with rejected cases only. It behaves the same as calling <em>then(undefined, onRejected)</em>. In fact, calling <em>catch(onRejected)</em> internally calls <em>then(undefined, onRejected)</em>.</p>



<p class="wp-block-paragraph">The catch method is used for error handling in promise composition. Since it returns a Promise, it can be chained in the same way as its sister method, then()</p>



<p class="wp-block-paragraph">Syntax:- catch(callback)</p>



<p class="wp-block-paragraph">Where the callback is a function called when the Promise is rejected. This function has one argument <em>error</em> – The rejection error.</p>



<h2 class="wp-block-heading"><strong>catch () Method</strong></h2>



<script src="https://gist.github.com/vijaypk376/ffcdec8b353af77dc76c124174aff7b2.js"></script>



<h2 class="wp-block-heading"><strong>finally () Method</strong></h2>



<p class="wp-block-paragraph">The finally() method returns a Promise. When the promise is settled, i.e either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully or rejected once the Promise has been dealt with.</p>



<p class="wp-block-paragraph">This helps to avoid duplicating code in both the promise&#8217;s then() and catch() handlers.</p>



<p class="wp-block-paragraph">Syntax:- finally(callback)</p>



<h2 class="wp-block-heading"><strong>finally () Method</strong></h2>



<script src="https://gist.github.com/vijaypk376/6313b37034386711f45503bcb708ca1f.js"></script>



<h2 class="wp-block-heading"><strong>Chaining</strong></h2>



<script src="https://gist.github.com/vijaypk376/37d902b6b443559b020399056cb5d998.js"></script>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/how-does-the-javascript-promise-work/">How does the JavaScript Promise work?</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.scmgalaxy.com/tutorials/how-does-the-javascript-promise-work/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JavaScript Cheat Sheet — JSON, Loops, and Promises</title>
		<link>https://www.scmgalaxy.com/tutorials/javascript-cheat-sheet%e2%80%8a-%e2%80%8ajson-loops-and-promises/</link>
					<comments>https://www.scmgalaxy.com/tutorials/javascript-cheat-sheet%e2%80%8a-%e2%80%8ajson-loops-and-promises/#respond</comments>
		
		<dc:creator><![CDATA[dharmendra K]]></dc:creator>
		<pubDate>Wed, 12 May 2021 15:20:55 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScrip]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[loops]]></category>
		<category><![CDATA[Promises]]></category>
		<guid isPermaLink="false">http://www.scmgalaxy.com/tutorials/?p=6226</guid>

					<description><![CDATA[<p>JavaScript is one of the most popular programming languages for web programming. In this article, we’ll look at the basic [&#8230;]</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/javascript-cheat-sheet%e2%80%8a-%e2%80%8ajson-loops-and-promises/">JavaScript Cheat Sheet — JSON, Loops, and Promises</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">JavaScript is one of the most popular programming languages for web programming.</p>



<p class="wp-block-paragraph">In this article, we’ll look at the basic syntax of modern JavaScript</p>



<h3 class="wp-block-heading">JSON</h3>



<p class="wp-block-paragraph">We can create JSON strings from JavaScript objects with the <code>JSON.stringify</code> method:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="323" height="134" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-3.jpg" alt="" class="wp-image-6231" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-3.jpg 323w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-3-300x124.jpg 300w" sizes="(max-width: 323px) 100vw, 323px" /></figure>



<p class="wp-block-paragraph">And we can convert JSON strings back to JavaScript objects with <strong><code>JSON.parse</code> :</strong></p>



const obj = JSON.parse(json);



<p class="wp-block-paragraph">We can use it to store data in local storage.</p>



<p class="wp-block-paragraph">We’ve to convert objects into strings first to store them.</p>



<p class="wp-block-paragraph">To store objects we call <strong><code>localStorage.setItem</code> :</strong></p>



<figure class="wp-block-image size-large"><img decoding="async" width="365" height="158" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-4.jpg" alt="" class="wp-image-6232" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-4.jpg 365w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-4-300x130.jpg 300w" sizes="(max-width: 365px) 100vw, 365px" /></figure>



<p class="wp-block-paragraph">The first argument is the key.</p>



<p class="wp-block-paragraph">And we can get data by their keys with <strong><code>getItem</code> :</strong></p>



localStorage.getItem(&#8216;json&#8217;)



<h3 class="wp-block-heading">Loops</h3>



<p class="wp-block-paragraph">avaScript comes with various kinds of loops.</p>



<p class="wp-block-paragraph">One kind of loop is the <strong><code>for</code> </strong>loop<strong>:</strong></p>



for (let i = 0; i < 10; i++) {
  console.log(i);
}



<p class="wp-block-paragraph">We can loop through any kind of iterable object with the <code><strong>for-of</strong></code> loop:</p>



for (let i of custOrder) {
  console.log(i);
}



<p class="wp-block-paragraph">Some iterable objects include arrays, strings, and node lists.</p>



<p class="wp-block-paragraph">Another kind of loop is the <code><strong>while</strong></code> loop:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="244" height="121" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-5.jpg" alt="" class="wp-image-6233"/></figure>



<p class="wp-block-paragraph">There’s also the <code><strong>do-while</strong></code> loop:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="281" height="135" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-6.jpg" alt="" class="wp-image-6234"/></figure>



<p class="wp-block-paragraph">The <code><strong>break</strong></code> keyword lets us end the loop early:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="340" height="148" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-7.jpg" alt="" class="wp-image-6235" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-7.jpg 340w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-7-300x131.jpg 300w" sizes="auto, (max-width: 340px) 100vw, 340px" /></figure>



<p class="wp-block-paragraph">The <strong><code>continue</code> </strong>keyword lets us skip to the next iteration:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="360" height="144" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-8.jpg" alt="" class="wp-image-6236" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-8.jpg 360w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-8-300x120.jpg 300w" sizes="auto, (max-width: 360px) 100vw, 360px" /></figure>



<h3 class="wp-block-heading">Data Types</h3>



<p class="wp-block-paragraph">JavaScript comes with various data types.</p>



<p class="wp-block-paragraph">They include:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="538" height="184" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-10.jpg" alt="" class="wp-image-6239" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-10.jpg 538w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-10-300x103.jpg 300w" sizes="auto, (max-width: 538px) 100vw, 538px" /></figure>



<h3 class="wp-block-heading">Objects</h3>



<p class="wp-block-paragraph">We can create an object with curly braces:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="461" height="179" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-11.jpg" alt="" class="wp-image-6240" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-11.jpg 461w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-11-300x116.jpg 300w" sizes="auto, (max-width: 461px) 100vw, 461px" /></figure>



<p class="wp-block-paragraph">It has properties and methods.</p>



<p class="wp-block-paragraph"><strong><code>this</code> </strong>is the <code><strong>student</strong></code> object itself.</p>



<p class="wp-block-paragraph">We can call <code><strong>fullName</strong></code> with <strong><code>student.fullName()</code> .</strong></p>



<p class="wp-block-paragraph">And we can assign values to properties with:</p>



student.age = 19;



<h3 class="wp-block-heading">Promises</h3>



<p class="wp-block-paragraph">We can create promises with the <code><strong>Promise</strong></code> constructor:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="605" height="212" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-12.jpg" alt="" class="wp-image-6241" srcset="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-12.jpg 605w, https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-12-300x105.jpg 300w" sizes="auto, (max-width: 605px) 100vw, 605px" /></figure>



<p class="wp-block-paragraph">We can <strong><code>resolve</code> </strong>to fulfill the promise with the sum.</p>



<p class="wp-block-paragraph">And we call <code><strong>reject</strong></code> to reject the promise with an error.</p>



<p class="wp-block-paragraph">Then we can call <code><strong>then</strong></code> to get the resolved value and <code><strong>catch</strong></code> to get the error values:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="286" height="149" src="https://www.scmgalaxy.com/tutorials/wp-content/uploads/2021/05/25-13.jpg" alt="" class="wp-image-6242"/></figure>



<h3 class="wp-block-heading">Conclusion</h3>



<p class="wp-block-paragraph">We can work with JSON, local storage, promises, and loops with JavaScript.</p>
<p>The post <a href="https://www.scmgalaxy.com/tutorials/javascript-cheat-sheet%e2%80%8a-%e2%80%8ajson-loops-and-promises/">JavaScript Cheat Sheet — JSON, Loops, and Promises</a> appeared first on <a href="https://www.scmgalaxy.com/tutorials">scmGalaxy</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.scmgalaxy.com/tutorials/javascript-cheat-sheet%e2%80%8a-%e2%80%8ajson-loops-and-promises/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
