<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
	
	>
<channel>
	<title>
	Comments on: Asynchronous Programming	</title>
	<atom:link href="https://jessewarden.com/2017/11/asynchronous-programming.html/feed" rel="self" type="application/rss+xml" />
	<link>https://jessewarden.com/2017/11/asynchronous-programming.html</link>
	<description>Software &#124; Fitness &#124; Gaming</description>
	<lastBuildDate>Thu, 09 Nov 2017 22:33:57 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: JP		</title>
		<link>https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250272</link>

		<dc:creator><![CDATA[JP]]></dc:creator>
		<pubDate>Thu, 09 Nov 2017 22:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5412#comment-250272</guid>

					<description><![CDATA[Wow now I get it- these are very interesting techniques! Thank you for great examples and a fast reply too!]]></description>
			<content:encoded><![CDATA[<p>Wow now I get it- these are very interesting techniques! Thank you for great examples and a fast reply too!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: JesterXL		</title>
		<link>https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250270</link>

		<dc:creator><![CDATA[JesterXL]]></dc:creator>
		<pubDate>Thu, 09 Nov 2017 03:05:48 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5412#comment-250270</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250268&quot;&gt;JP&lt;/a&gt;.

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;const maybeCow = cow =&gt;
  new Promise( (success, failure) =&gt;
    cow === &#039;cow&#039; ? success(&#039;it is a cow&#039;)
    : failure(`it&#039;s not a cow, it is: ${cow}`));&lt;/code&gt;&lt;/pre&gt;

You pass in a String, and if it doesn&#039;t equal &#039;cow&#039;, it&#039;ll throw. That&#039;s bad because once you go to using the async/await syntax, you now are forcing users to use a try/catch.

Instead, you either create a Promise that always succeeds:

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;const maybeCow = cow =&gt;
new Promise( success =&gt;
  cow === &#039;cow&#039; ? success({ok: true, data: &#039;it is a cow&#039;})
  : success({ok: false, error: `it&#039;s not a cow, it is: ${cow}`}));&lt;/code&gt;&lt;/pre&gt;

When used as a Promise, there is no need to ever use the .catch, because it always returns a resolved Promise in the .catch:

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;maybeCow(&#039;chicken&#039;)
.then(result =&gt; console.log(&quot;result:&quot;, result))&lt;/code&gt;&lt;/pre&gt;

It&#039;s more useful in the async/await, that&#039;d look like:

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;const {ok, error, data} = await maybeCow(&#039;chicken&#039;);&lt;/code&gt;&lt;/pre&gt;

Now your code looks more like Go or Lua in error handling.

The other way is to just create a simple wrapper so &quot;Look homey, I don&#039;t know where I got this Promise, but uh, can you make sure it doesn&#039;t throw and just let me know if it worked or not?&quot;

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;const sureThing = promise =&gt;
  promise
  .then(result =&gt; ({ok: true, result}))
  .catch(error =&gt; Promise.resolve({ok: false, error}));&lt;/code&gt;&lt;/pre&gt;

And to use it:

&lt;pre&gt;&lt;code class=&quot;lang-javascript&quot;&gt;const go = async () =&gt; {
    const {ok, error, result} = await sureThing(maybeCow(&#039;chicken&#039;));
    console.log(&quot;ok:&quot;, ok);
    console.log(&quot;error:&quot;, error);
    console.log(&quot;result:&quot;, result);
};

go();
// ok: false
// error: it&#039;s not a cow, it is: chicken
// result: undefined&lt;/code&gt;&lt;/pre&gt;]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250268">JP</a>.</p>
<pre><code class="lang-javascript">const maybeCow = cow =>
  new Promise( (success, failure) =>
    cow === 'cow' ? success('it is a cow')
    : failure(`it's not a cow, it is: ${cow}`));</code></pre>
<p>You pass in a String, and if it doesn&#8217;t equal &#8216;cow&#8217;, it&#8217;ll throw. That&#8217;s bad because once you go to using the async/await syntax, you now are forcing users to use a try/catch.</p>
<p>Instead, you either create a Promise that always succeeds:</p>
<pre><code class="lang-javascript">const maybeCow = cow =>
new Promise( success =>
  cow === 'cow' ? success({ok: true, data: 'it is a cow'})
  : success({ok: false, error: `it's not a cow, it is: ${cow}`}));</code></pre>
<p>When used as a Promise, there is no need to ever use the .catch, because it always returns a resolved Promise in the .catch:</p>
<pre><code class="lang-javascript">maybeCow('chicken')
.then(result => console.log("result:", result))</code></pre>
<p>It&#8217;s more useful in the async/await, that&#8217;d look like:</p>
<pre><code class="lang-javascript">const {ok, error, data} = await maybeCow('chicken');</code></pre>
<p>Now your code looks more like Go or Lua in error handling.</p>
<p>The other way is to just create a simple wrapper so &#8220;Look homey, I don&#8217;t know where I got this Promise, but uh, can you make sure it doesn&#8217;t throw and just let me know if it worked or not?&#8221;</p>
<pre><code class="lang-javascript">const sureThing = promise =>
  promise
  .then(result => ({ok: true, result}))
  .catch(error => Promise.resolve({ok: false, error}));</code></pre>
<p>And to use it:</p>
<pre><code class="lang-javascript">const go = async () => {
    const {ok, error, result} = await sureThing(maybeCow('chicken'));
    console.log("ok:", ok);
    console.log("error:", error);
    console.log("result:", result);
};

go();
// ok: false
// error: it's not a cow, it is: chicken
// result: undefined</code></pre>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: JP		</title>
		<link>https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250268</link>

		<dc:creator><![CDATA[JP]]></dc:creator>
		<pubDate>Wed, 08 Nov 2017 23:14:14 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5412#comment-250268</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250267&quot;&gt;JP&lt;/a&gt;.

&quot;You can ensure this is less of a problem by ensure all Promises you create never fire catch, and the success method returns an Either.&quot;

I was trying to google the example of solution with Either but all I see is examples in Scala, not any in RamdaJS. Can you please update the article with the code example of how to implement error handling in async/await with JavaScript Either?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250267">JP</a>.</p>
<p>&#8220;You can ensure this is less of a problem by ensure all Promises you create never fire catch, and the success method returns an Either.&#8221;</p>
<p>I was trying to google the example of solution with Either but all I see is examples in Scala, not any in RamdaJS. Can you please update the article with the code example of how to implement error handling in async/await with JavaScript Either?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: JP		</title>
		<link>https://jessewarden.com/2017/11/asynchronous-programming.html/comment-page-1#comment-250267</link>

		<dc:creator><![CDATA[JP]]></dc:creator>
		<pubDate>Wed, 08 Nov 2017 21:29:38 +0000</pubDate>
		<guid isPermaLink="false">http://jessewarden.com/?p=5412#comment-250267</guid>

					<description><![CDATA[Great stuff as always Jesse! Thank you for your time!]]></description>
			<content:encoded><![CDATA[<p>Great stuff as always Jesse! Thank you for your time!</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
