<?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"><channel><title><![CDATA[Sebin's Thoughts]]></title><description><![CDATA[Sebin's Thoughts]]></description><link>https://blog.sebins.in</link><generator>RSS for Node</generator><lastBuildDate>Fri, 05 Jun 2026 22:01:52 GMT</lastBuildDate><atom:link href="https://blog.sebins.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Using Async.Js autoInject method to create complex asynchronous calls]]></title><description><![CDATA[Async.JS is a useful library to manage asynchronous JavaScript calls. It can be used in wide variety of ways to solve many common problems.
The following example shows you on how to use async.autoInject to combine many inter dependent asynchronous fu...]]></description><link>https://blog.sebins.in/using-asyncjs-autoinject-method-to-create-complex-asynchronous-calls</link><guid isPermaLink="true">https://blog.sebins.in/using-asyncjs-autoinject-method-to-create-complex-asynchronous-calls</guid><category><![CDATA[asynchronous]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[async]]></category><category><![CDATA[version control]]></category><category><![CDATA[Node.js]]></category><dc:creator><![CDATA[Sebin P Johnson]]></dc:creator><pubDate>Sun, 08 Aug 2021 15:12:37 GMT</pubDate><content:encoded><![CDATA[<p>Async.JS is a useful library to manage asynchronous JavaScript calls. It can be used in wide variety of ways to solve many common problems.</p>
<p>The following example shows you on how to use <strong>async.autoInject</strong> to combine many inter dependent asynchronous functions seamlessly</p>
<pre><code><span class="hljs-keyword">const</span> <span class="hljs-keyword">async</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">'async'</span>);

<span class="hljs-comment">/**
 * adds 3 to a number after 2 seconds
 * @param text
 * @returns {Promise&lt;unknown&gt;}
 */</span>
<span class="hljs-keyword">const</span> add3 = <span class="hljs-function">(<span class="hljs-params"><span class="hljs-built_in">number</span> = <span class="hljs-number">1</span></span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
        <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
            resolve(<span class="hljs-built_in">number</span> + <span class="hljs-number">3</span>)
        }, <span class="hljs-number">2000</span>)
    })
}

<span class="hljs-comment">/**
 * Multiplies after 2 seconds
 * @param a
 * @param b
 * @returns {Promise&lt;unknown&gt;}
 */</span>
<span class="hljs-keyword">const</span> mulitply = <span class="hljs-function">(<span class="hljs-params">a,b</span>) =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> {
        <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
            resolve(a*b)
        }, <span class="hljs-number">2000</span>)
    })
}

<span class="hljs-comment">/**
 * A control flow using async.autoInject
 * @type {string}
 */</span>
<span class="hljs-keyword">const</span> timerId = <span class="hljs-string">'asyncAuto'</span>
<span class="hljs-built_in">console</span>.time(timerId);
<span class="hljs-keyword">async</span>.autoInject({
    fivePlus3: <span class="hljs-keyword">async</span> () =&gt; add3(<span class="hljs-number">5</span>),
    onePlus3: <span class="hljs-keyword">async</span> () =&gt; add3(<span class="hljs-number">1</span>),
    multiplyTheAboveTwoProps: <span class="hljs-keyword">async</span> (fivePlus3,onePlus3) =&gt; mulitply(fivePlus3,onePlus3), <span class="hljs-comment">// takes the resolved values of the fivePlus3,onePlus3</span>
    add3ToFinal: <span class="hljs-keyword">async</span> (multiplyTheAboveTwoProps) =&gt; add3(multiplyTheAboveTwoProps), <span class="hljs-comment">// takes the resolved value of  multiplyTheAboveTwoProps</span>
    add3TofivePlus3: <span class="hljs-keyword">async</span> (fivePlus3) =&gt; add3(fivePlus3) <span class="hljs-comment">// takes the resolved value of fivePlus3</span>
}).then(<span class="hljs-function"><span class="hljs-params">r</span> =&gt;</span> {
    <span class="hljs-built_in">console</span>.timeEnd(timerId);
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'finished controlled flow'</span>, r)
})
</code></pre><p> Find a working example =&gt; <a target="_blank" href="https://runkit.com/sebinpj/async-js-examples-for-es2017---async-auto">RunKit</a> </p>
<blockquote>
<p>PS: This is the first time i ever wrote anything</p>
</blockquote>
]]></content:encoded></item></channel></rss>