<?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: Mohammed khalid</title>
    <description>The latest articles on DEV Community by Mohammed khalid (@mohammedkhalid96).</description>
    <link>https://dev.to/mohammedkhalid96</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%2F349190%2F9cdbb6ed-8110-4faf-8d64-7826ac8bb173.jpg</url>
      <title>DEV Community: Mohammed khalid</title>
      <link>https://dev.to/mohammedkhalid96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohammedkhalid96"/>
    <language>en</language>
    <item>
      <title>In trouble with promises? Don't worry here's your saviour</title>
      <dc:creator>Mohammed khalid</dc:creator>
      <pubDate>Sat, 18 Apr 2020 09:18:25 +0000</pubDate>
      <link>https://dev.to/mohammedkhalid96/in-trouble-with-promises-don-t-worry-here-s-your-saviour-2dg</link>
      <guid>https://dev.to/mohammedkhalid96/in-trouble-with-promises-don-t-worry-here-s-your-saviour-2dg</guid>
      <description>&lt;p&gt;This article is just an extremely simple intro to ES6 promises to make it clearer, and I hope that at the end of this article, I have helped you to be more familiar with JavaScript promises.&lt;/p&gt;

&lt;p&gt;Promises in JavaScript is just like the promises in our real life, Promises in our real-life have two possibilities: fulfilled or unfulfilled. That's the same thing in JavaScript promises, there're three possibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pending: initial state, neither fulfilled nor rejected.&lt;/li&gt;
&lt;li&gt;fulfilled: meaning that the operation completed successfully.&lt;/li&gt;
&lt;li&gt;rejected(unfulfilled): meaning that the operation failed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In JavaScript, Promises are used to handle asynchronous operations.&lt;/p&gt;




&lt;p&gt;A promise can be created using Promise constructor&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2a35lrtbsxhnwlu1qx3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2a35lrtbsxhnwlu1qx3d.png" alt="Alt Text"&gt;&lt;/a&gt; &lt;br&gt;
"Promise constructor takes only one argument, a callback function, this callback function takes two arguments, resolve and reject, and if everything went well then call resolve. If desired operations do not go well then call reject"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Promises can be consumed by registering functions using .then and .catch methods.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then() is invoked when a promise is either resolved or rejected. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;catch() is invoked when a promise is either rejected or some error has occurred in execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjg30puzzsm7tbiabdcgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjg30puzzsm7tbiabdcgq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Example &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7bl726doeqjceoibtb7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fq7bl726doeqjceoibtb7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
"Expected output - He didn't wash his hand"&lt;/p&gt;




&lt;p&gt;Promise methods&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Promise.reject()&lt;br&gt;
"Promise.reject returns a rejected promise"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Promise.resolve()&lt;br&gt;
"Promise.resolve returns a resolved promise"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Promise.all()&lt;br&gt;
"Promise.all takes an array of promises. Then it gets resolved when all the promises get resolved or reject with the reason of the first passed promise that rejects"&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz0o0kmuqypmvsd96je0s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz0o0kmuqypmvsd96je0s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
"Expected output - (3) ["Post 1 is done", "Post 2 is done", "Post 3 is done"]"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promise.race()
"Promise.race is just like promise.all except for it'll return as soon as the first one is completed instead of waiting for everything to complete"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbshkrb4jd29rccn12a3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbshkrb4jd29rccn12a3y.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
"Expected output - Post 1 is done"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promise.allSettled()
"Promise.allSettled returns a Promise that resolves after all of the given promises have either resolved or rejected, with an array of objects that each describes the outcome of each promise"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh2k6zklo2xuv6vbdbwms.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fh2k6zklo2xuv6vbdbwms.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
"Expected output - (3) [{…}, {…}, {…}] 0: {status: "fulfilled", value: "Post 1 is done"} 1: {status: "fulfilled", value: "Post 2 is done"} 2: {status: "fulfilled", value: "Post 3 is done"}"&lt;/p&gt;

&lt;p&gt;I hope I have helped you to be more familiar with JavaScript promises. and if you want to go deeper I recommend these sources for you&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com./javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261" rel="noopener noreferrer"&gt;https://medium.com./javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com./better-programming/understanding-promises-in-javascript-13d99df067c1" rel="noopener noreferrer"&gt;https://medium.com./better-programming/understanding-promises-in-javascript-13d99df067c1&lt;/a&gt;&lt;/p&gt;

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