<?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 Viktor Pyskunov on Medium]]></title>
        <description><![CDATA[Stories by Viktor Pyskunov on Medium]]></description>
        <link>https://medium.com/@vpyskunov?source=rss-810cf34097bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uAlF4_lfTstJb-j1yZln3A.jpeg</url>
            <title>Stories by Viktor Pyskunov on Medium</title>
            <link>https://medium.com/@vpyskunov?source=rss-810cf34097bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 03:21:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vpyskunov/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[Async Function in JavaScript]]></title>
            <link>https://medium.com/@vpyskunov/async-function-in-javascript-c657fc6b9a26?source=rss-810cf34097bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/c657fc6b9a26</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[asynchronous-javascript]]></category>
            <category><![CDATA[asynchronous]]></category>
            <dc:creator><![CDATA[Viktor Pyskunov]]></dc:creator>
            <pubDate>Fri, 29 Mar 2019 11:29:55 GMT</pubDate>
            <atom:updated>2019-04-10T14:43:51.571Z</atom:updated>
            <content:encoded><![CDATA[<h3>Async Functions in JavaScript</h3><p>According to <a href="https://en.wikipedia.org/wiki/Async/await">Wikipedia</a>, Async/Await is a syntactic feature of programming languages that allows an asynchronous function to be structured in a way similar to an ordinary synchronous function. In this post we will talk about <strong>async</strong> <strong>keyword</strong> in JavaScript.</p><p>Async keyword can be placed before a function definition. This way we can define an async function in JavaScript.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*Lr5TugXus7o2dKY6h7lVBw.png" /><figcaption>Defining an async function in JavaScript</figcaption></figure><p>Async function is a function that executes asynchronously via the JavaScript Event Loop.</p><h3>Returning Non-Promise Values From An Async Function</h3><p>The important thing to know about async functions in JavaScript is that async function always returns a promise. If async function returns a non-promise value, then JavaScript automatically wraps it into a promise. A resolved promise with that same value. Let me show what I mean by the following example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*woR8A5gmN_-WtnRLYxXM6w.png" /><figcaption>Returning a non-promise value from an async function</figcaption></figure><p>This function returns a string “Hello World”. However, if we invoke sayHelloWorld function, we will get a promise as a result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZsNikZiP6lgJutUIgeocMA.png" /><figcaption>Invoking async function in Google Chrome Developer Tools</figcaption></figure><p>As you can see on the screenshot above, sayHelloWorld function returned a resolved promise with the value “Hello World”.</p><h3>Returning A Promise From An Async Function</h3><p>If an async function returns something that is already a promise,<br>then JavaScript will not make any transformations on it. For example, let’s return a promise from an async function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*ffUdjtti1rTV8CdWiJJaPg.png" /><figcaption>Returning a promise from an async function</figcaption></figure><p>If we invoke function f and check its return value right away (without waiting 5 seconds), we will notice that this function returns a pending promise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3d6q3SJVIYfusETTC0hIgg.png" /></figure><p>You can see that variable x contains a pending promise at the moment.<br>But if we wait 5 seconds and check it again, the promise will be resolved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YYTyoRse8pmS63E0nsW3Hg.png" /></figure><p>As you can see, now variable x contains a resolved promise with the value true. So async function hasn’t made any modifications on the return value this time.</p><p>We can also return a rejected promise from an async function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*ui8FiYUDynC7f4fouSxZUA.png" /></figure><p>Then function f will return a rejected promise without any modifications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W6H3Fa7zupUyMmpQSxaThg.png" /></figure><p>As you can see, async function returned the same rejected promise. It didn’t convert it to a resolved promise or anything else. It just returned the promise as it was.</p><h3>Throwing An Error Inside An Async Function</h3><p>If an error is thrown inside an async function, this function returns a rejected promise. In this situation PromiseValue will be equal to the Error instance that was thrown.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*Rdiy_BmuJAc_ImrljW6_rg.png" /><figcaption>Throwing an error inside as async function</figcaption></figure><p>If we invoke function f, we will get a promise with the PromiseStatus equal to “rejected” and PromiseValue equal to “Error: Some error”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X5q-KR2qfNEfaczXbr1aMg.png" /><figcaption>Invoking function f in Google Chrome Developer Tools</figcaption></figure><p>However, if I return an error (not throw) from an async function, it will convert this error to a resolved promise:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*78knJw8eBwSCvKC-F6lfdQ.png" /><figcaption>Returning an instance of Error from an async function</figcaption></figure><p>So async keyword ensures that the function returns a promise,<br>and wraps non-promise values into a promise.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c657fc6b9a26" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>