{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/promises","result":{"data":{"markdownRemark":{"html":"<p><i>I cannot help but hear the song Promises by 3LW when we talk about Promises in Javascript.</i></p>\n<p>Analogy inspired by <a href=\"&#x27;https://javascript.info/promise-basics&#x27;\">Javascript Modern Tutorial</a> :</p>\n<p>Imagine that you are Rihanna, and fans ask you day and night for your upcoming single, but you keep ignoring them.</p>\n<p>To get some relief, you promise to upload it when you are finished. You give your fans a Fenty list. They can fill in their email addresses, so that when the album becomes available, all subscribed parties instantly receive it. And even if something goes very wrong, say, you decided to only focus on your makeup and clothing brand, and you don't want to upload an album anymore, they will still be notified.</p>\n<p>Everyone is happy: you, because the people don’t spam your instagram comments, and fans, because they believe that you will one day upload a summer album.</p>\n<p>Below is directly from <a href=\"&#x27;https://javascript.info/promise-basics&#x27;\">Javascript Modern Tutorial</a>:\nThis is a real-life analogy for things we often have in programming:</p>\n<ol>\n<li>A “producing code” that does something and takes time. For instance, some code that loads the data over a network. That’s Rihanna.</li>\n<li>A “consuming code” that wants the result of the “producing code” once it’s ready. Many functions may need that result. These are the “fans”.</li>\n<li>A promise is a special JavaScript object that links the “producing code” and the “consuming code” together. In terms of the analogy: this is the “subscription list”. The “producing code” takes whatever time it needs to produce the promised result, and the “promise” makes that result available to all of the subscribed code when it’s ready.</li>\n</ol>\n<p><strong>Note</strong></p>\n<ul>\n<li>The analogy isn’t accurate, because JavaScript promises are more complex than a simple subscription list: they have additional features and limitations. But it’s fine to begin with.</li>\n</ul>\n<h3>So what is a promise?</h3>\n<p>A promise is an object that represents a value that is or will be the result of an asynchronous process.\nA promise is always in one of three states: <i>pending, resolved, or rejected</i>.\nWhen a promise is resolved or rejected, we say that it is settled. Once a promise is settled, it cannot transition to another state.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token comment\">//Fetch at a high level, quick intro</span>\n<span class=\"token keyword\">const</span> songPromise <span class=\"token operator\">=</span> <span class=\"token function\">fetch</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"http://rihanna.com/new-album\"</span><span class=\"token punctuation\">)</span>\n\nsongPromise\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">songs</span> <span class=\"token operator\">=></span> album<span class=\"token punctuation\">.</span><span class=\"token function\">json</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">//will only fire when the data comes back successfully</span>\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">songs</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>songs<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">//Then is a method, it takes up to two callbacks</span>\n  <span class=\"token punctuation\">.</span><span class=\"token function\">catch</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">//catches any errors that happen along the way</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<h4>The Promise Constructor</h4>\n<p>ES6 has promises built in.</p>\n<p>To create your own promise, you create a variable, and store a new promise in it.</p>\n<p>The Promise constructor takes one function which passes you resolve and reject.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-javascript line-numbers\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> myPromise <span class=\"token operator\">=</span> <span class=\"token keyword\">new</span> <span class=\"token class-name\">Promise</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">resolve<span class=\"token punctuation\">,</span> reject</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">reject</span><span class=\"token punctuation\">(</span><span class=\"token function\">Error</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"The Album will be released June 2020\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">1000</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\nmyPromise\n  <span class=\"token punctuation\">.</span><span class=\"token function\">then</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">data</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>data<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">.</span><span class=\"token function\">catch</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">err</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>The Promise constructor is only used for promisifying ascynchronous functions. It invokes resolve and revoke callbacks to asynchronously send values, and there are no retutn values.</p>","excerpt":"I cannot help but hear the song Promises by 3LW when we talk about Promises in Javascript. Analogy inspired by Javascript Modern Tutorial : Imagine that you are…","frontmatter":{"date":"10 March, 2020","path":"/blog/promises","title":"Promises"},"fields":{"readingTime":{"text":"3 min read"}}}},"pageContext":{}}}