<?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 Sarah Q on Medium]]></title>
        <description><![CDATA[Stories by Sarah Q on Medium]]></description>
        <link>https://medium.com/@esskew213?source=rss-12bc398f278f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*8X_5dZEO-Xb5hrXUw6mu6w.jpeg</url>
            <title>Stories by Sarah Q on Medium</title>
            <link>https://medium.com/@esskew213?source=rss-12bc398f278f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 03:52:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@esskew213/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[The important bits of web performance, for new devs]]></title>
            <link>https://medium.com/@esskew213/the-important-bits-of-web-performance-for-new-devs-e6fbef998bf?source=rss-12bc398f278f------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6fbef998bf</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-performance]]></category>
            <dc:creator><![CDATA[Sarah Q]]></dc:creator>
            <pubDate>Thu, 26 May 2022 08:31:20 GMT</pubDate>
            <atom:updated>2022-05-26T08:38:53.150Z</atom:updated>
            <content:encoded><![CDATA[<h4>What I wish I’d learnt <em>before</em> diving into any frontend frameworks.</h4><p>This post was inspired by my foray into the popular module bundler <a href="https://webpack.js.org/">Webpack</a>—yet another one of those (many) things they don’t teach you about in coding bootcamp.</p><p>More accurately, I started out wanting to learn Webpack, and then found myself going down the Rabbit Hole of <em>Why</em>, as in:</p><ol><li><strong>Why do we even need Webpack? </strong>Oh, it’s for module bundling…</li><li><strong>Why do we even need module bundling?</strong> Ah, it helps improve our page load times…</li><li><strong>Why does bundling improve page load times?</strong> For one, if we combine smaller modules of code into bigger bundles, we can reduce the number of requests we make to the server, which is generally better for performance. Beyond that, <a href="https://developer.mozilla.org/en-US/docs/Glossary/Code_splitting">splitting code smartly</a> also means we can choose to download the essential bundles <em>first</em> to get our page up and running quickly, and have the remaining bundles load lazily after that.</li></ol><p>At this point, I realised I knew nothing about the fundamentals of web performance, and this was in fact impeding my mastery of React (substitute for any other frontend framework). For instance, I had no idea what the point of <a href="https://reactjs.org/docs/react-api.html#reactsuspense">React.lazy() or </a><a href="https://reactjs.org/docs/react-api.html#reactsuspense">React.Suspense</a> was, so I simply never used them.</p><blockquote>This wasn’t a great approach, because to be a good web developer, you presumably need to care about good web performance. After all, what’s the point of being able to create a site with React/Angular/Vue if it takes forever to load on every click?</blockquote><h3>So here’s what I learnt about web performance</h3><p>I’m a big fan of writing notes by hand — you can check them out below and even save them if they’re helpful! Otherwise, here’s a quick, 128-word summary of what I learnt, plus links to the resources I used:</p><ul><li><strong>Web performance is objectively measurable but is also a perceived experience — </strong>for instance, we can use lazy loading to make our websites interactive <em>before</em> all content has been loaded. That gives users the <em>impression</em> that the website is responsive. On a related note, I highly recommend this famous article on <a href="https://www.nngroup.com/articles/ten-usability-heuristics/">principles of user interaction design</a> by Jakob Nielsen.</li><li><a href="https://developer.mozilla.org/en-US/docs/Learn/Performance/Perceived_performance"><strong>Common metrics for web performance</strong></a><strong> </strong>— First Meaningful Paint, Largest Contentful Paint, Time to interactive, etc. <a href="https://pagespeed.web.dev/"><strong>PageSpeed by Google</strong></a> is super helpful for generating performance reports.</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/Performance/How_long_is_too_long"><strong>Recommended web performance timings </strong></a><strong>—</strong> apparently these are a thing?! In particular, I thought aiming to <strong>provide feedback to user interaction within 50 to 200ms</strong> was a good guideline.</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/Performance/Critical_rendering_path"><strong>Critical Rendering Path</strong></a><strong> —</strong> a fundamental concept we need to understand to optimise time to first render.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uKcoMK7kATTQkit5_myukA.png" /><figcaption>Handwritten notes on the basics of web performance</figcaption></figure><h3>Conclusion</h3><p>My summary of web performance wasn’t comprehensive by any means, but it was enough to give me newfound appreciation for performance-related features in frameworks like React and NextJS. I hope it was useful to you too! Now I can go back to learning Webpack in pea—wait, why do we need to use the <a href="https://babeljs.io/docs/en/">Babel</a> loader?</p><h3>😉</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6fbef998bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Advice from a senior dev: Stop using the implicit “else”]]></title>
            <link>https://javascript.plainenglish.io/advice-from-a-senior-dev-stop-using-the-implicit-else-2a2ecf0a3583?source=rss-12bc398f278f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a2ecf0a3583</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sarah Q]]></dc:creator>
            <pubDate>Mon, 23 May 2022 03:34:38 GMT</pubDate>
            <atom:updated>2022-05-31T02:00:07.873Z</atom:updated>
            <content:encoded><![CDATA[<h3>Advice From a Senior Dev: Stop Using the Implicit “else”</h3><h4>I think there’s a tendency — among junior devs, at least — to think that the shorter the code, the better. But that’s not always the case.</h4><p>I think there’s a tendency — among junior devs, at least — to think that the shorter the code, the better. (<em>Repeat after me: Shorter is NOT always better.) </em>I certainly fall prey to this line of thinking even though I know it’s erroneous; that’s why I thought the implicit else was the best thing since sliced bread when I discovered it.</p><p>That was until a senior dev schooled me otherwise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vwiBsZADO3_QHuE4" /><figcaption>Photo by <a href="https://unsplash.com/@benwhitephotography?utm_source=medium&amp;utm_medium=referral">Ben White</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>. This was me being mindblown.</figcaption></figure><h3>What is this implicit &#39;else&#39;?</h3><p>Skip to the next section if you’re already familiar with the concept. For everyone else:</p><p><strong>Using the implicit </strong><strong>else with </strong><strong>if statements takes advantage of the fact that </strong><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return"><strong>return statements end function execution</strong></a><strong>. In certain cases, this lets us eliminate </strong><strong>else statements, resulting in shorter code.</strong></p><p>Have a look at the toy functions giveMeFood and giveMeFoodImplicit below. We don’t <em>technically</em> need an else because if you fed me chocolate (just saying), the function would return <em>‘yum’</em> early and stop executing. And if you didn’t, then it would return <em>‘no thanks’</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f40d633ca32d1aaa14cfcf5c737fcdc1/href">https://medium.com/media/f40d633ca32d1aaa14cfcf5c737fcdc1/href</a></iframe><p>In fact, I can add even more if...return statements in lieu of if...else, with a default return at the end! Seems cleaner, no? Does that mean we can kiss the else goodbye forever?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45ffd5a71f34c61d5c3321935652e71a/href">https://medium.com/media/45ffd5a71f34c61d5c3321935652e71a/href</a></iframe><h3>Why and when to avoid the implicit &#39;else&#39;</h3><p>There are definitely occasions to use the implicit else, but this is what a senior dev suggested to me:</p><ol><li><strong>Elses help to keep your conditionals explicit so that they are extensible and easily understood by others (including your future self).</strong></li><li><strong>You can use the final </strong><strong>else as a catch-all or to handle errors if an argument is passed in that your function wasn’t built to handle.</strong></li></ol><p>Here’s an example to demonstrate what he means.</p><p>Say we have a function that checks what country a user selected, and returns a dialing code. At the moment, checkCountryCode is built only to handle &quot;United States&quot; and &quot;United Kingdom&quot; as arguments. It’s tempting to use the implicit else to omit the second conditional entirely and shorten the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b52463a4a847bc9257d12fb071e949d7/href">https://medium.com/media/b52463a4a847bc9257d12fb071e949d7/href</a></iframe><p>The problem arises if we later want to extend the function to handle additional countries — say Uruguay and Uzbekistan. It’s easy to forget or get confused by what the implicit return is supposed to be handling (in this case, it’s supposed to return +44 if the input is &quot;United Kingdom&quot;)!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e505d492ad8fa4a485dc5e10cc87cddb/href">https://medium.com/media/e505d492ad8fa4a485dc5e10cc87cddb/href</a></iframe><p>Now here’s the code rewritten with explicit elses. It’s certainly longer, but now if someone passes in a country we haven’t accounted for yet, an error will be raised — something particularly useful if this function was nested in a much larger codebase.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8eab1aff70c011c0c54b809a7b15973a/href">https://medium.com/media/8eab1aff70c011c0c54b809a7b15973a/href</a></iframe><p>And yes, you <em>could</em> throw an error without the final else, but here’s the fundamental question we need to ask ourselves:</p><p><strong>Does omitting </strong><strong>elses left, right and center <em>actually</em> make my code easier to understand? If there were a bunch of </strong><strong>if...returns and then a </strong><strong>throw statement appearing out of the blue at the bottom, would that be intuitive for others to read?</strong></p><h3>In conclusion</h3><p>I’m sharing this tip because I think it’s an interesting take on the implicit vs explicit else. Choosing one over the other has implications for longer-term maintainability and extensibility that weren’t immediately obvious to a junior dev like me. Of course, it’s up to us to decide what the best structure for our own code is — but it’s always good to know what the various options are! :’)</p><p><em>More content at </em><a href="https://plainenglish.io/"><strong><em>PlainEnglish.io</em></strong></a><em>. Sign up for our </em><a href="http://newsletter.plainenglish.io/"><strong><em>free weekly newsletter</em></strong></a><em>. Follow us on </em><a href="https://twitter.com/inPlainEngHQ"><strong><em>Twitter</em></strong></a><em> and </em><a href="https://www.linkedin.com/company/inplainenglish/"><strong><em>LinkedIn</em></strong></a><em>. Check out our </em><a href="https://discord.gg/GtDtUAvyhW"><strong><em>Community Discord</em></strong></a><em> and join our </em><a href="https://inplainenglish.pallet.com/talent/welcome"><strong><em>Talent Collective</em></strong></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a2ecf0a3583" width="1" height="1" alt=""><hr><p><a href="https://javascript.plainenglish.io/advice-from-a-senior-dev-stop-using-the-implicit-else-2a2ecf0a3583">Advice from a senior dev: Stop using the implicit “else”</a> was originally published in <a href="https://javascript.plainenglish.io/">JavaScript in Plain English</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get comfy with Javascript destructuring in under 5 minutes]]></title>
            <link>https://medium.com/@esskew213/the-important-bits-of-javascript-destructuring-in-under-5-minutes-7586bf1e734a?source=rss-12bc398f278f------2</link>
            <guid isPermaLink="false">https://medium.com/p/7586bf1e734a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sarah Q]]></dc:creator>
            <pubDate>Fri, 20 May 2022 02:27:18 GMT</pubDate>
            <atom:updated>2022-05-20T03:56:18.851Z</atom:updated>
            <content:encoded><![CDATA[<p>When I was learning the very basics of Javascript, I observed that many of us in class didn’t really have a good understanding of <strong>destructuring</strong>. It’s one of those ES6 features that seem deceptively easy to grasp — but my shaky knowledge of the concept came back to bite me when I started learning React with hooks, and we had to pass props around like so many hot potatoes.</p><p>Whether or not you know React, destructuring is worth taking a few minutes to wrap your brain around. It can make our code cleaner and more concise — just don’t get confused by the syntax.</p><h3>What is destructuring?</h3><p>Destructuring is a way of “unpacking” values from <strong>arrays</strong> or properties from <strong>objects</strong>, and assigning them to variables. That doesn’t mean much, so let’s see how it works.</p><h3>How does it work?</h3><blockquote>In a nutshell: On the <strong>left-hand side </strong>of the equals sign, we define <strong>which variables we want to unpack</strong> from the array or object that is on the <strong>right-hand side</strong>.</blockquote><h4>Arrays</h4><p><strong>For arrays</strong>, the variables are assigned according to their position in the array.</p><pre>const arr = [1, 2, 3];</pre><pre>let [first, second, third] = arr;<br>console.log(first); // 1<br>console.log(second); // 2</pre><p>If there are more items in the array than the variables declared, the former simply won’t be assigned.</p><pre>const arr = [1, 2, 3, 4];</pre><pre>let [first] = arr;<br>console.log(first) // 1;</pre><p>You can even skip variables you’re not interested in, like so:</p><pre>let [first, ,third] = arr;<br>console.log(third) // 3</pre><h4>Objects</h4><p><strong>For objects</strong>, we identify the values we want by their key. We also have the option to <strong>rename the variable</strong>.</p><p><strong>For both objects and arrays, we can also specify a default value that will be used if the value unpacked is </strong><strong>undefined</strong>. <em>(Note that if the value unpacked is </em><em>null or an empty string </em><em>&#39;&#39; , the default value is </em><strong><em>not</em></strong><em> used!)</em></p><p>In the example below, we destructure the name and age variables, and rename them to username and userAge respectively. We also want to assign default values of Anonymous and 12.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dD0yWd90kxfn-vfm9SBS_w.png" /><figcaption>Destructuring assignment for an object, with variable renaming and default values</figcaption></figure><h3>Okay, so… what’s the <em>point</em>?</h3><p>For starters, one obvious benefit is demonstrated in the code below (as suggested in <a href="https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/">this superb article</a> by Nick Fitzgerald, Jason Orendorff). Which of the two methods of assigning variables from the array is easier to write / read?</p><pre>const arr = [1, 2, 3];</pre><pre><strong>// DO YOU PREFER THIS?</strong><br>let first = arr[0];<br>let second = arr[1];<br>let third = arr[2];</pre><pre><strong>// OR THIS?</strong><br>let [first, second, third] = arr;</pre><blockquote>You may still be unimpressed, but destructuring assignment becomes particularly useful if we need to<strong> repeatedly reference certain object properties in our code</strong> — say if a user submitted a new sign-up form and we need to do something with the data.</blockquote><p>Here’s another example. If we want to access the user’s interests more than once, it might be easier to destructure it once so that we don’t have to type user.bio.interests every time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MS-PZmG8_P3eLRJK9LzV5g.png" /><figcaption>Destructuring can save us from having to repeatedly type out user.bio.interests</figcaption></figure><h3>Nested destructuring</h3><p>In the example above you may have noticed that I’m actually accessing a nested property (interestsinside bio).</p><blockquote><strong>Observe how the syntax for the nested destructuring mirrors the structure of the original object! </strong>Once we realise this, writing out the code for nested destructuring becomes almost intuitive.</blockquote><p>Here’s yet another example, where I’ve destructured some data on a fake Medium post. Pay attention to the stats and tags destructuring in particular:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rnuz_aPYu6fWAX-6eCupWA.png" /><figcaption>Bonus: Note how I’ve collected the array of tags and assigned it to the variable allTags with the spread syntax […]</figcaption></figure><p>Fret not if you’re still confused by the syntax. Practising with a few examples will make it all clear! Meanwhile, here’s how I’ve come to think of nested destructuring:</p><blockquote>To get a value of interest from an <strong>object</strong>, we specify to the level of its key, enclosed within curly braces {}. (Renaming it is optional.) To get a value in an <strong>array</strong>, there’s no key yet, so we must declare and assign a new variable name within the square brackets [].</blockquote><p>I hope that helps! (And, if it didn’t, pretend you didn’t read it. 😉)</p><h3>Bonus: A note on copying nested objects / arrays with destructuring and the spread syntax</h3><p>It’s important to understand that<strong> destructuring assignments leave nested arrays / objects referring to the same space in memory as the original</strong>. In practicality what this means is that modifying the array / object assigned to your new variable will affect the original too!</p><p>Here’s a concrete example, going back to our mediumPost. If I try to extract the array of tags into a new variable called myTag and then modify it there, I’m going to end up changing the original array of tags too.</p><pre>let {<br>    details: {<br>    tags: myTags,<br>  },<br>} = mediumPost;</pre><pre>myTags[0] = &#39;NOT Javascript&#39;;</pre><pre>console.log(mediumPost.details.tags[0]); // &#39;NOT Javascript&#39;</pre><p>Oh no! But we can implement a quick fix with the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax">spread operator</a>, which creates a shallow copy of the array that we can modify as we like:</p><pre>let {<br>    details: {<br>    tags: [...myTags],<br>  },<br>} = mediumPost;</pre><pre>myTags[0] = &#39;NOT Javascript&#39;;</pre><pre>console.log(mediumPost.details.tags[0]); // &#39;Javascript&#39;</pre><p>That brings me to the end of this primer on destructuring. If I’ve made any errors, please point them out! And feel free to play around with destructuring in the code editor below (pull up the console to view the output).</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fjs-yxzgfw%3FdevToolsHeight%3D33%26embed%3D1%26file%3DnestedDestructuring.js%26hideExplorer%3D1%26hideNavigation%3D1&amp;display_name=StackBlitz&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fjs-yxzgfw%3FdevToolsHeight%3D33%26embed%3D1%26file%3DnestedDestructuring.js%26hideExplorer%3D1%26hideNavigation%3D1&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Fsocial_editor-1e53e71ce7e2963fcef1b44837c4570c3fb10c8cb64b194a6eafa4e145d10edc.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/434810f4c42cb442d2edfc8d6b95250a/href">https://medium.com/media/434810f4c42cb442d2edfc8d6b95250a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7586bf1e734a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Javascript Promises in plain English (I promise!)]]></title>
            <link>https://medium.com/@esskew213/javascript-promises-in-plain-english-i-promise-dae80c79cffc?source=rss-12bc398f278f------2</link>
            <guid isPermaLink="false">https://medium.com/p/dae80c79cffc</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Sarah Q]]></dc:creator>
            <pubDate>Thu, 19 May 2022 10:40:17 GMT</pubDate>
            <atom:updated>2022-05-19T22:19:49.908Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-KsGcxCLhaDLLBet" /><figcaption>Photo by <a href="https://unsplash.com/@andrewwwpetrov?utm_source=medium&amp;utm_medium=referral">Andrew Petrov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a>.</figcaption></figure><p>Hello! This is the first of what I hope will be a series of short posts explaining the oft-neglected basics of Javascript. I started with Promises because I realised that after I started sprinkling async / await liberally in my code like the delicious <a href="https://medium.com/@matt.mcalister93/async-and-await-syntactic-sugar-for-promises-in-javascript-aee7ace36d14"><strong>syntactic sugar that it is</strong></a>, I actually forgot what Promises were about entirely. And yet they are, as Mozilla Developer Network <a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises">dramatically puts it</a>, <em>“the foundation of asynchronous programming in modern JavaScript”</em>.</p><p>I mean, oof. Better get cracking then!</p><h3>So, what is a Promise?</h3><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Here’s</a> what MDN has to say about it:</p><blockquote>A Promise is an object representing the eventual completion or failure of an asynchronous operation.</blockquote><p>As a junior dev, I read this and found it… unnecessarily cryptic. So here’s my alternative interpretation:</p><blockquote>A Promise basically does what it does in the “plain English” sense — <strong>it’s a way of promising some value that we don’t have <em>yet</em></strong>. And, as with all promises we mortals make, they can either be fulfilled, or… well, broken. (Ouch). Those two outcomes will have different consequences.</blockquote><h3>Okay, but… why?</h3><p>If at this point you’re wondering what the point of Promises is, we need to take a step back to refresh our memory of asynchronous programming.</p><blockquote>Async programming is important because some operations that we need our program to do — like retrieving data from a remote server to display in our app—take time to complete.</blockquote><p>If we were to use a normal synchronous function to do this, the next line of code wouldn’t run until we received a response, which wouldn’t be good for our app’s user experience!</p><p>Instead, we use async programming, and Promises, to get around this. The following sections detail how.</p><h3>What a Promise looks like</h3><p>First off, we need to create a <strong>new Promise object</strong>. Into this Promise we pass a <strong>function</strong>, which take two parameters that are themselves functions — resolve and reject.</p><p>If we call resolve() , the Promise will be considered <strong>fulfilled</strong>. If we call reject(), the Promise is considered <strong>rejected</strong>. And before we call either, its status will be marked <strong>pending. </strong>Check out the function willIDoMyHomeworkbelow, which will return a Promise every time it is called.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NhB61GQ2941g_o58dprsgA.png" /><figcaption>50–50 chance of me keeping my promise to do my homework? That’s pretty high.</figcaption></figure><p>You’ll notice I passed values (in this case, an object with a key of dataand value of `yeah` or `nah` respectively) to the resolve and reject functions. These will be used in the handlers that are chained to the Promise object.</p><h3>Chain…? Handlers…? Eh?</h3><p>Right! Here we come to the great thing about Promises:</p><blockquote>Promises let us “queue” up functions (i.e. handlers) that will be executed only when the asynchronous operation has succeeded or failed. What’s more, we can pass data from the original Promise to the handlers!</blockquote><p>We use the .then and .catch methods to do this queueing (you can read more about them <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then">here</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch">here</a>). Going back to our example about Promising to do our homework:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7W_jSGprs7xN5AWgk2KL-g.png" /><figcaption>The function passed to the .then method will run when the Promise is fulfilled, and the one passed to .catch will run if the Promise is rejected.</figcaption></figure><p>Pretty cool, right? In reality we probably wouldn’t just be logging the response—we might be displaying it or doing other fancy things to it.</p><p>But that’s only half the story. Remember how we mentioned <strong>chaining</strong>? It’s made possible by the fact that the .then method returns —wait for it—<strong><em>another Promise</em></strong>!</p><blockquote>The implication of this is that we can chain on another .then, <em>without the need for nesting</em>, with a handler that runs when that second Promise is resolved, and chain on a third function on after that, so on and so forth until we’re happy as clams.</blockquote><h3>In summary</h3><ul><li>Promises are the foundation of asynchronous programming. Async programming is important because we want our app to be responsive even when an operation (like retrieving data) is taking time to complete!</li><li>Promises allow us to chain on / queue up functions that will only run when the asynchronous operation completes.</li><li>The function passed to .then will run if the Promise is <strong>resolved</strong>, and the function passed to .catch runs if the Promise is <strong>rejected</strong>. Before either occurs, the Promise’s status is pending.</li><li>.then and .catch themselves can return Promises, a feature which allows us to chain on subsequent handlers without having to nest them!</li></ul><p>I leave you with a toy example that nevertheless is intended to illustrate how Promises work. The code is similar to the example we talked about above, except that the handlers passed to .then and .catch do some very basic DOM manipulation.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fjs-xpwwpv%3Fembed%3D1%26file%3Dindex.js%26hideExplorer%3D1&amp;display_name=StackBlitz&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fjs-xpwwpv%3Fembed%3D1%26file%3Dindex.js%26hideExplorer%3D1&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Fsocial_editor-1e53e71ce7e2963fcef1b44837c4570c3fb10c8cb64b194a6eafa4e145d10edc.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/99a2afe5f21ccf6c25de4a9334d13c0c/href">https://medium.com/media/99a2afe5f21ccf6c25de4a9334d13c0c/href</a></iframe><p>And that’s all for my very first post on Medium! If I made any mistakes, please let me know — I’m here to learn from the best. :’)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dae80c79cffc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>