<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Chaining on Nathaniel Inman</title><link>https://nathanielinman.com/tags/chaining/</link><description>Recent content in Chaining on Nathaniel Inman</description><generator>Hugo -- 0.147.7</generator><language>en-us</language><copyright>Nathaniel Inman</copyright><lastBuildDate>Sun, 06 Jan 2019 00:00:00 +0000</lastBuildDate><atom:link href="https://nathanielinman.com/tags/chaining/index.xml" rel="self" type="application/rss+xml"/><item><title>Async Await Chaining</title><link>https://nathanielinman.com/async-await-chaining/</link><pubDate>Sun, 06 Jan 2019 00:00:00 +0000</pubDate><guid>https://nathanielinman.com/async-await-chaining/</guid><description>&lt;p>Creating synchronously chained functions is straight forward. &lt;a href="https://codepen.io/NathanielInman/pen/f2c6f77986160188091575625ba1604b">&lt;strong>Codepen&lt;/strong>&lt;/a>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-gdscript3" data-lang="gdscript3">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> UserCollectionSync &lt;span style="color:#f92672">=&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> find(id&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#39;&amp;#39;&lt;/span>){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;1&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> update(){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;2&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> save(){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;3&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>UserCollectionSync
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>find()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>update()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>save();
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>But when we decide to just throw some &amp;lsquo;async&amp;rsquo; &amp;lsquo;await&amp;rsquo; flags on those functions everything doesn&amp;rsquo;t work. &lt;strong>FOLLOWING CODE IS WRONG:&lt;/strong> &lt;a href="https://codepen.io/NathanielInman/pen/c8306ad8a473d6cfe79d26cf9fee1624">&lt;strong>Codepen&lt;/strong>&lt;/a>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-gdscript3" data-lang="gdscript3">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">//&lt;/span> delay is used to represent some asynchronous work
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>function delay(ms){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> new Promise(resolve &lt;span style="color:#f92672">=&amp;gt;&lt;/span> setTimeout(resolve, ms));
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>} &lt;span style="color:#f92672">//&lt;/span>end delay()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">const&lt;/span> UserCollectionAsync &lt;span style="color:#f92672">=&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> async find(id&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#e6db74">&amp;#39;&amp;#39;&lt;/span>){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> await delay(&lt;span style="color:#ae81ff">1000&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;1&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> async updateFor(options&lt;span style="color:#f92672">=&lt;/span>{}){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> await delay(&lt;span style="color:#ae81ff">50&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;2&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> },
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> async save(){
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> await delay(&lt;span style="color:#ae81ff">50&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> document&lt;span style="color:#f92672">.&lt;/span>writeln(&lt;span style="color:#e6db74">&amp;#39;3&amp;#39;&lt;/span>);
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> this;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>UserCollectionAsync
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>find()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>updateFor(&lt;span style="color:#e6db74">&amp;#39;&amp;#39;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#f92672">.&lt;/span>save();
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The reason it fails is because when the &amp;lsquo;find()&amp;rsquo; is called it returns a promise that hasn&amp;rsquo;t resolved instead of the &amp;lsquo;UserCollectionAsync&amp;rsquo; object (Factory pattern.) Therefor you can&amp;rsquo;t find a function &amp;lsquo;update()&amp;rsquo; on a promise. One common way of solving the issue is by allowing the factory to maintain a queue and force the user to add &amp;lsquo;done()&amp;rsquo; at the end of the chain: &lt;a href="https://codepen.io/NathanielInman/pen/cdb094f0c87a5a7673d2c67fb7f1a88f">&lt;strong>Codepen&lt;/strong>&lt;/a>.&lt;/p></description></item></channel></rss>