<?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 olexandr on Medium]]></title>
        <description><![CDATA[Stories by olexandr on Medium]]></description>
        <link>https://medium.com/@alSkachkov?source=rss-b4fd7b959967------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*K64fUyUYjJVhUaMe.jpeg</url>
            <title>Stories by olexandr on Medium</title>
            <link>https://medium.com/@alSkachkov?source=rss-b4fd7b959967------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 13:09:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alSkachkov/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[ES2018: ‘finally’ method in native Promise]]></title>
            <link>https://medium.com/@alSkachkov/es2018-finally-method-in-native-promise-bb865834abba?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb865834abba</guid>
            <category><![CDATA[es2015]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Thu, 09 Nov 2017 20:27:19 GMT</pubDate>
            <atom:updated>2017-11-09T20:27:19.991Z</atom:updated>
            <content:encoded><![CDATA[<p>TC39 committee is working on improving async computation in javascript. Promise (<a href="https://medium.com/@alSkachkov/1-async-functions-in-javascript-promise-d770bd11bf87">more about promises</a>) was added in ES6, later in ES2017 was added async function(<a href="https://codeburst.io/2-async-functions-in-javascript-async-await-a905d5aae73c">more about async/await function</a>). Recently proposal for Promise.prototype.finally reached to Stage 3. I’m expecting to see <a href="https://github.com/tc39/proposal-promise-finally">Promise.prototype.finally</a> and <a href="https://github.com/tc39/proposal-async-iteration">async iterator &amp; generator</a> in next ES2018 spec, because both features are delivered by main browsers. I happy to be part of the cool hacker team that is delivering those features to WebKit.</p><p>Lets look why Promise.prototype.finally is useful.</p><h4><strong>Promise.prototype.finally</strong></h4><p>In javascript as in many C-like languages exists try/catch/finally statement, This statement allows handle exception in synchronous control flow. Async control flow that relies on promises, natively support only try/catch flow and finally can be implemented only by adding the same code to the both paths, success and exception for instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/740162d6aea7eb36d5030d70c1b60777/href">https://medium.com/media/740162d6aea7eb36d5030d70c1b60777/href</a></iframe><p>This approach leads to duplication of the code. To handle this issue TC39 committee approved spec that adds finally method to Promise object, so Promise method become symmetrical to try/catch/finally statement. finally has callback as parameter and return Promise, that allows to chain promises. There is example how previous snipped would look with using finally</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a7ba5dbc875d7fa9d313c8f53823566/href">https://medium.com/media/2a7ba5dbc875d7fa9d313c8f53823566/href</a></iframe><p>In snipped, finally allows to remove duplication of the code from handlers. Also it is great that finally can easily be chained</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d98d6ae3cd52a04067e067debe6db40/href">https://medium.com/media/2d98d6ae3cd52a04067e067debe6db40/href</a></iframe><p>finally still is invoked if error handler presents in then or catch is used, but catch would not be invoked if then contains error handler.</p><p>Important to mention that finally in contrast with catch allows transfer value returned from previous promise to next next or catch</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7d7920bd573e1f4a58f091c9130efbd/href">https://medium.com/media/b7d7920bd573e1f4a58f091c9130efbd/href</a></iframe><h3>How to use now</h3><p>As for now Promise.prototype.finally supported only from <strong><em>Safari Technical Preview 40</em></strong> and <strong><em>Chrome Canary 63</em></strong>, <strong><em>nodejs</em></strong> <strong>v8.6</strong> supports it as well. I expect that soon this browsers release this feature in stable versions.</p><p>To support Promise.prototype.finally in the earlier browsers can be used <em>“shim”.</em> It was developed by spec’s author and can be found by link <a href="https://github.com/es-shims/Promise.prototype.finally">https://github.com/es-shims/Promise.prototype.finally</a> or install by <strong><em>npm</em></strong>.</p><pre>npm install <a href="https://github.com/es-shims/Promise.prototype.finally">Promise.prototype.finally</a></pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb865834abba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to run the latest WebKit ( Safari ) on Windows]]></title>
            <link>https://medium.com/@alSkachkov/how-to-load-the-latest-webkit-on-windows-962a9219c1e1?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/962a9219c1e1</guid>
            <category><![CDATA[safari]]></category>
            <category><![CDATA[webkit]]></category>
            <category><![CDATA[jscs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[windows]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Wed, 04 Oct 2017 09:36:52 GMT</pubDate>
            <atom:updated>2018-03-22T09:24:24.122Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are working on Mac or Linux you can easily download WebKit or Safari Technical Preview from this link <a href="https://webkit.org/downloads/">https://webkit.org/downloads/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*bCrZl8IXt29qONZLLubpjA.png" /><figcaption>WebKit</figcaption></figure><p>But sometimes it is required to check how some JavaScript or HTML/CSS will look like in the latest Webkit and in next Safari on Windows environment. Before developer has to build WebKit by self, and it is quite time consuming operation for such small operation as check something in Webkit on Window:</p><h3>Brian Terlson on Twitter</h3><p>@othermaciej @alSkachkov Can you please make the windows builds of JSC available? This would be a great help for the JS community. Currently hard to test with JSC.</p><h3>Brian Terlson on Twitter</h3><p>@alSkachkov @othermaciej Yeah, that&#39;s the part I want to avoid. Builds take forever and when you build infrequently, getting into a buildable state is not trivial :(</p><p>But after the latest changes in webkit build pipeline, this can be done in easier way:</p><p><a href="https://webkit.org/blog/7978/introducing-webkit-build-archives/">Introducing WebKit Build Archives</a></p><p>Because build artifacts stored for 14 days, they can be downloaded from storage. There is a steps how it can be done by self.</p><p><a href="https://build.webkit.org/builders/Apple%20Win%20Release%20%28Build%29">Buildbot: Apple Win Release (Build)</a></p><p>There is list of Webkit Windows builds:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*i9Wl2WbGZjkJLavCmUMhbw.png" /><figcaption>Build console</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*SYL2_ob1FsJBIm-igBWtZQ.png" /><figcaption>Build steps</figcaption></figure><p>There link to log where builds are stored:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*7uHv054H5w00sGuxikxpWQ.png" /><figcaption>Build logs</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2NsyjwBAwyP7jt4Vc4xEig.png" /><figcaption>Link to build</figcaption></figure><p>So we can use this link to download build, be prepared it quite big more that 200 Mb.</p><p>There can be found MinBrowser.exe and jsc.exe that allow to test latest WebKit features. Also be aware that to run jsc.exe Apple Application support(AAS) libs should be installed, I’ve installed iTunes for that, and possible cygwin as well. I’ve checked on my dev machine and this components were already installed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=962a9219c1e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[2. Async Functions in JavaScript: async / await]]></title>
            <link>https://codeburst.io/2-async-functions-in-javascript-async-await-a905d5aae73c?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/a905d5aae73c</guid>
            <category><![CDATA[async-functions]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[es7]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Tue, 27 Jun 2017 09:04:08 GMT</pubDate>
            <atom:updated>2017-06-27T12:53:07.478Z</atom:updated>
            <content:encoded><![CDATA[<p>In the <a href="https://medium.com/@alSkachkov/1-async-functions-in-javascript-promise-d770bd11bf87#.ny1pdjffl">previous article</a> we took a look to the <strong><em>Promise.</em></strong></p><p>In this article we will look to an <a href="https://tc39.github.io/ecmascript-asyncawait/">async function</a> that is part of the ES2017 specification.</p><p>Recently all main browsers released implementation of the async/await function. Async function is <a href="https://en.wikipedia.org/wiki/Coroutine">coroutine</a> that allow to work with <strong><em>Promises</em></strong> by using javascript language syntax and allow to implement nonlinear control flow in javascript. Currently async control flow in javascript can be expressed with several features: event, callback, streams and promises, and soon with async generator.</p><p>There is an simple example of declaration and using of the async function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/29597f8c76d2d9dec775bb67282b5613/href">https://medium.com/media/29597f8c76d2d9dec775bb67282b5613/href</a></iframe><h4><strong>Syntax</strong></h4><p>The specification of the async function adds new keyword <em>async</em> and a operator await<strong>.</strong> Any async function declaration should start from <em>async</em> keyword, the await operator can be used only within async function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8401313f21c6394786e969450c2dd450/href">https://medium.com/media/8401313f21c6394786e969450c2dd450/href</a></iframe><p>The <em>await</em> operator is used to wait for a resolved <em>Promise</em>, that is returned by the async function. In general we can use <em>await</em> with any value. When we use <em>Promise</em>, the<em>await</em> returns resolved value, if <em>Promise</em> is rejected the <em>await</em> throw exception with rejected value. In case if value is not <em>Promise</em> the <em>await</em> operator returns the same value. It is important that <strong><em>await</em></strong> is cause of execution the async function to pause, and it waits until promise’s resolution. In case if the <em>await</em> is used with not promise value, the execution of function is suspended for short period of time, until promise resolves the value, so it means that code that is after async function invocation will be executed earlier than code that is placed after <em>await</em> within async function. See example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1382523046ab803bb30d3d22d1c74b3/href">https://medium.com/media/a1382523046ab803bb30d3d22d1c74b3/href</a></iframe><p>In this example the <em>await</em> operator causes <em>async</em> function execution to pause in <em>await &#39;boo&#39;</em>, and waits for the <em>Promise</em>&#39;s resolution in our case just <em>Promise.resolve(&#39;boo&#39;)</em>, and allows to execute <em>console.log</em> just after <em>foo()</em>. In our case async function will be resumed as soon as all linear code is executed, because we use <em>await</em> with simple value.</p><p>If the <em>Promise</em> that is awaited is rejected, the <em>await</em> expression throws the rejected value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4c3fa10d30a836e91022b0c0a2a0393/href">https://medium.com/media/d4c3fa10d30a836e91022b0c0a2a0393/href</a></iframe><h4><strong>Runtime</strong></h4><p>Lets overview how async function is executed. In general without await async function works as common function, except that async function always returns result of execution wrapped inro promise object. The value is returned by async function will be in fulfill part of the <em>Promise</em> handler, exception will be returned in reject part of <em>Promise</em> handler. But there is a big difference when the operator <em>await</em> is used. In the simplified version <em>await</em> is convert function in big <em>SWITCH/CASE</em>, where each <em>await</em> can be interpreted as the end of the current <em>case</em> block, with self invoking within <em>Promise</em> fulfill handler. As <em>Promise</em> would be resolved we invoke function with next step. There is example of the simplified version of the async function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74a21532e24cef03881b67f8fcedb9da/href">https://medium.com/media/74a21532e24cef03881b67f8fcedb9da/href</a></iframe><p>It is important to mention that any errors within async function body lead to reject promise that returned by async function, so exception in async function can’t be caught by try/catch block, error can be caught by promise’s handler for reject or catch<strong>.</strong></p><h4><strong>Usage</strong></h4><p>As mentioned before, async function returns promise, so the flow of execution can be organized by <em>Promise’s</em> object methods: <em>all</em>, <em>race</em> and and can be chained inside of the fulfill promise handler.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39583de1c56d1c158046e6c095ced54a/href">https://medium.com/media/39583de1c56d1c158046e6c095ced54a/href</a></iframe><p>Sometimes it s necessary to check if current function is async function. <em>typeof</em> is return for such object value <em>‘function’</em>, but there can be used well-known symbol<strong> </strong><strong><em>Symbol.toStringTag</em></strong>.<strong><em> </em></strong>According to spec each async function should contain value <em>‘AsyncFunction’</em>, in property with name <strong><em>Symbol.toStringTag</em></strong> :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/442b24178d31ce290b490eecbcfb1a53/href">https://medium.com/media/442b24178d31ce290b490eecbcfb1a53/href</a></iframe><h4><strong>Unwrapping promise</strong></h4><p>Async function always unwraps the returned <em>Promise</em>, so if a returned value is a promise, or received from invocation of another async function without <strong><em>await</em></strong>, the value would not appear until returned promise will be resolved.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/723530385b3d432abe8b121d1e928d6e/href">https://medium.com/media/723530385b3d432abe8b121d1e928d6e/href</a></iframe><h4><strong>Async arrow function</strong></h4><p>The keyword <em>async</em> can be used with arrow function expression, so it allows to make a declaration of async function shorter.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ccd7e2c62290cce9d4aad2ff9bfcc0f/href">https://medium.com/media/6ccd7e2c62290cce9d4aad2ff9bfcc0f/href</a></iframe><h4><strong>Async function in class</strong></h4><p>Async function can be used as method within class also it can be used within constructor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bee6fd3a8213ba1a640184752e2d47e/href">https://medium.com/media/4bee6fd3a8213ba1a640184752e2d47e/href</a></iframe><p>When async function is used within constructor it is important that <em>super</em> invoked before any await, otherwise it lead to error when class’s constructor is invoked</p><p>It is interesting that spec allows to use method with name <em>async</em> and <em>await</em></p><h4><strong>Using in project</strong></h4><p>Currently most of the modern browsers support async/await function, see <a href="http://caniuse.com/#feat=async-functions">canIuse</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zzC22fTS0_9Fe1O7ZDaI6A.png" /></figure><p>If you’re targeting older browsers, Babel can transpile <strong><em>async/await functions</em></strong> for you. The plugin <a href="https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-regenerator"><strong>babel-plugin-transform-regenerator</strong></a><strong>t</strong> contains transpiler for async function, and it is included into <strong><em>es2015</em></strong> and <strong><em>es2017</em></strong> preset.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a905d5aae73c" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/2-async-functions-in-javascript-async-await-a905d5aae73c">2. Async Functions in JavaScript: async / await</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interfaces in JavaScript with ES6 Symbol. Naive implementation]]></title>
            <link>https://codeburst.io/interfaces-in-javascript-with-es6-naive-implementation-91b703110a09?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/91b703110a09</guid>
            <category><![CDATA[symbols]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[interfaces]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Wed, 21 Jun 2017 10:46:12 GMT</pubDate>
            <atom:updated>2018-06-01T14:18:12.393Z</atom:updated>
            <content:encoded><![CDATA[<p>As we know, javascript has own approach to implement OOP, this is different from approaches in C# and Java, and in earliest days before ES6 javascript does not support class, and inheritance was implemented by prototypes or some custom implementation. After class became a part of the javascript and started widely used there, many people start require make one more step, and make interface as the part of the javascript. To respond this people was proposed <a href="https://github.com/michaelficarra/proposal-first-class-protocols">first class protocols</a>, this spec on <strong>Stage 1. </strong>So it will not be so soon till it will be part of the language. How we can implement this in current state of the javascript:</p><ol><li><em>Do not implement and just use</em><strong><em> Duck Typing</em></strong><em>.</em> Sometimes, this approach is good enough, but there are a lot of<strong> </strong>cases where this approach can lead to some unexpected behavior of the application.</li><li><em>Use library or transpiler for interface.</em> There are bunch of the different libs that allow to implement interface within javascript code. I’ll not stop on this approach, because in our project we decided do not rely on any external lib.</li><li><em>Use TypeScript. </em>TypeScript as superset of javascript support of the interface, but switching the our project to TypeScript was a big change for us.</li><li><em>Use </em><em>Proxy. </em>Unfortunately<em> it is not supported by Babel: </em>Due to the limitations of ES5, Proxies cannot be transpiled or polyfilled. See support in various JavaScript engines</li><li><em>Use data type Symbol. </em>In ES6 spec was added new data type Symbol . It has very <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Symbol">important characteristic</a>:</li></ol><p>Every symbol value returned from Symbol() is unique</p><p>So based on this characteristic we built our approach to support interface in project.</p><p>We create property in class with Symbol name that return object with the implementation of the interface. Using of the Symbol prevent from existing in object some property that named in the same way as interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b52d622da2c18e4419c7549f52ed63a2/href">https://medium.com/media/b52d622da2c18e4419c7549f52ed63a2/href</a></iframe><p>Yeah, it is quite naive approach, but allows to answer question if object implement interface. We can go father and use Symbol as name of each function in interface, but in our project we decided that it is enough to use Symbol only for name of the interface.</p><p>Actually ES6 generator also use this approach to implement iterator/generator in any object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/731fff160137269cffcd2350c834f9a6/href">https://medium.com/media/731fff160137269cffcd2350c834f9a6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91b703110a09" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/interfaces-in-javascript-with-es6-naive-implementation-91b703110a09">Interfaces in JavaScript with ES6 Symbol. Naive implementation</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JS: escaped characters in variable name]]></title>
            <link>https://medium.com/@alSkachkov/js-escaped-characters-in-variable-name-ba3251289459?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba3251289459</guid>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Mon, 19 Jun 2017 21:00:41 GMT</pubDate>
            <atom:updated>2017-06-19T21:00:41.305Z</atom:updated>
            <content:encoded><![CDATA[<p>It is interesting, that <a href="https://tc39.github.io/ecma262/#sec-identifier-names-static-semantics-early-errors"><em>ES6 spec</em></a> allows to use escape characters in name of the variable. So it should work in modern browsers Safari, Chrome, FireFox and Edge:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08db334cc664cf435aa8b56ecc8f9d3d/href">https://medium.com/media/08db334cc664cf435aa8b56ecc8f9d3d/href</a></iframe><p>But there is a issue in IE11:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*ynPJdQusYejvBcDOoeh-KA.png" /><figcaption>IE11</figcaption></figure><p>In one of the stackoverflow.com question I found funny <a href="https://mothereff.in/js-variables">example</a> of valid variable name:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4dafb7fe012db162eee92adc7b297f4/href">https://medium.com/media/b4dafb7fe012db162eee92adc7b297f4/href</a></iframe><p>Also it is important to notice that escape character is not allowed to use in keywords so <em>retur\u006E</em> will lead to syntax error:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5c8cad06b3be008806466adbfca8504/href">https://medium.com/media/a5c8cad06b3be008806466adbfca8504/href</a></iframe><p>So we need to be careful when we use escaped character in variable name, I would recommend do not use them at all, to prevent unexpected result during parsing or in runtime</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba3251289459" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using async/await function in Angular 1.5 + Babel 6]]></title>
            <link>https://medium.com/@alSkachkov/using-async-await-function-in-angular-1-5-babel-6-387f7c43948c?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/387f7c43948c</guid>
            <category><![CDATA[es2017]]></category>
            <category><![CDATA[await]]></category>
            <category><![CDATA[async]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Mon, 19 Jun 2017 08:06:54 GMT</pubDate>
            <atom:updated>2017-06-19T08:06:54.381Z</atom:updated>
            <content:encoded><![CDATA[<p>As async/await function is became a part of ES2017 spec, our team decided to start use it in project. It is great that async/await function also supported by Babel, so we could play with in current project that target some old browsers. So we make prototype of the solution with angular service and controller that use async function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b8a37b461d1ed9bd3bf148d3b6d6331/href">https://medium.com/media/8b8a37b461d1ed9bd3bf148d3b6d6331/href</a></iframe><p>It works fine except one problem that async function is executed out of angular digest it returned data appears only after some force scope update, by scope.apply, but code start looks not very simple. After some research it was decided to use implementation of <strong><em>decorators</em></strong>. Babel contains transpiler for <strong><em>decorators. </em></strong>It has difference from current version of the spec, but it allow to solve our problem. So we create simple decorator and that add rooScope.$applyAsync to promise chain into target async method of the controller.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e231a078e19cfe0744abd744ed78478a/href">https://medium.com/media/e231a078e19cfe0744abd744ed78478a/href</a></iframe><p>So after this modification decorator controller, still looks ok and we do need add scope.$apply in each method</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6e4551e601c6da753b5e33f6f739cc6/href">https://medium.com/media/e6e4551e601c6da753b5e33f6f739cc6/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=387f7c43948c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[1. Async Functions in JavaScript: Promise]]></title>
            <link>https://medium.com/@alSkachkov/1-async-functions-in-javascript-promise-d770bd11bf87?source=rss-b4fd7b959967------2</link>
            <guid isPermaLink="false">https://medium.com/p/d770bd11bf87</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ecmascript-6]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[promises]]></category>
            <category><![CDATA[async]]></category>
            <dc:creator><![CDATA[olexandr]]></dc:creator>
            <pubDate>Tue, 07 Mar 2017 21:53:36 GMT</pubDate>
            <atom:updated>2017-03-07T21:53:36.190Z</atom:updated>
            <content:encoded><![CDATA[<p>As you know in recent versions of <strong>ECMAScript</strong>, javascript received new features that extend asynchronous capabilities of the language.</p><p>Most interested features are: <a href="https://tc39.github.io/ecma262/#sec-promise-objects"><strong><em>Promise</em></strong></a>, <a href="https://tc39.github.io/ecma262/#sec-async-function-definitions"><strong><em>Async Function</em></strong></a><strong><em>, </em></strong><a href="https://tc39.github.io/proposal-async-iteration/"><strong><em>Async Iterator</em></strong></a></p><p>Let’s take a look at all of them, but let’s start from <strong><em>Promise</em></strong>, because it is basis for Async Function and Async iterator. Async Function and Async iterator will be described in next articles.</p><p>In browser’s javascript all calls to server are executed in asynchronous mode, before we register callbacks that handle the result of server calls. But when we have several calls to the server that are linked by common logic, we might need to use callback in callbacks and etc. and it can lead to a mess in the code. One of the alternatives to the callback is <strong><em>Promise. Promise</em></strong> allows to add to asynchronous operation as many handlers as we want, also it allows to have one handler for several async operations or execute linked asynchronous operations one by one with using a chain of promises.</p><p>In MDN the Promise is described as:</p><blockquote>“The <strong><em>Promise</em></strong> object is used for asynchronous computations. A Promise represents a value which may be available now, or in the future, or never.”</blockquote><p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise</a></p><p>There are several libs that allow using the <strong><em>Promises</em></strong> in JavaScript.</p><ul><li><a href="https://api.jquery.com/promise/"><strong><em>jQuery</em></strong></a></li><li><a href="https://github.com/kriskowal/q"><strong><em>q</em></strong></a></li><li><a href="http://bluebirdjs.com/docs/getting-started.html"><strong><em>Bluebird</em></strong></a></li></ul><p>From<em> ECMAScript 6 the </em><strong><em>Promises </em></strong>is part of the language <a href="https://tc39.github.io/ecma262/#sec-promise-objects">specification</a>.</p><blockquote>A <strong>Promise</strong> is an object that is used as a placeholder for the eventual results of a deferred (and possibly asynchronous) computation.</blockquote><p>A <strong><em>Promise</em></strong> can have one of three internal states:</p><ul><li><em>pending</em>: initial state, not fulfilled or rejected.</li><li><em>fulfilled</em>: meaning that the operation was completed successfully.</li><li><em>rejected</em>: meaning that the operation failed.</li></ul><p>A <strong><em>Promise</em></strong> that is <em>fulfilled</em> or <em>rejected</em> is called <em>settled</em>.</p><p>As for now it is impossible to know the internal state of a <strong><em>Promise</em></strong> from JavaScript,</p><p>but it can be viewed in console.log</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZDpF0LVLjZT8fucINWOryA.png" /><figcaption>Safari 10.0.3</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Md7Oj1lTIk6I177uwabOCw.png" /><figcaption>Chrome Canary 58</figcaption></figure><p>In modern browser, a <strong><em>Promise</em></strong> is internal javascript object.</p><p>A <strong><em>Promise</em></strong> can be created in any place of code in the following way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecca488882ed3ee2770ad5eaf181783d/href">https://medium.com/media/ecca488882ed3ee2770ad5eaf181783d/href</a></iframe><p>Where resolve and reject should be invoked when <strong><em>Promises</em></strong> are successfully <em>fulfilled</em> or <em>rejected</em> because of an error or unexpected behaviour.</p><p>For instance:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d4361026a849cfe4912b1f6feb83942/href">https://medium.com/media/2d4361026a849cfe4912b1f6feb83942/href</a></iframe><p>So the <strong><em>Promise</em></strong> will be successfully <em>fulfilled</em> in 50ms with value <em>success</em> when resolve callback is invoked.</p><p>Any time we can subscribe to the promises and receive the result of resolving or rejection by adding a callback in the method then:</p><p>Let’s look at the following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b748398e59d1443bef6f2d620a9599a3/href">https://medium.com/media/b748398e59d1443bef6f2d620a9599a3/href</a></iframe><p><strong>Promise.resolve</strong> — returns a promise that is resolved with the given value, if the value is <strong><em>than</em></strong>able object, the returned promise will follow <strong><em>than</em></strong>able and will be fulfilled with value from <strong><em>than</em></strong>able, otherwise the promise will be fulfilled with the given value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56e860f4d75cab3247e22585edb307f1/href">https://medium.com/media/56e860f4d75cab3247e22585edb307f1/href</a></iframe><p><strong>Promise.reject</strong> — return promise that <em>rejects</em> with value</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33bb63d3af8c4ab617618e15505179ee/href">https://medium.com/media/33bb63d3af8c4ab617618e15505179ee/href</a></iframe><p><strong>Promise.all </strong>— returns a promise that will be <em>fulfilled</em> when all given promises are resolved or rejected at least once.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7eb39a96b524de93267a514730db12d/href">https://medium.com/media/c7eb39a96b524de93267a514730db12d/href</a></iframe><p><strong>Promise.race</strong> — returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects, with the value or reason from that promise.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5381cdaaf9e66eb8fff1e2a6a0767ca9/href">https://medium.com/media/5381cdaaf9e66eb8fff1e2a6a0767ca9/href</a></iframe><p>Chain of promises:</p><p>Promises can be ordered in a chain if callback for the than method returns a promise:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1804eaffe6ed684775822e9efb9eeaa4/href">https://medium.com/media/1804eaffe6ed684775822e9efb9eeaa4/href</a></iframe><p><strong><em>How to use promises now:</em></strong></p><p>Most of the modern browsers support the native <strong>Promises.</strong> See on <a href="http://caniuse.com/#feat=promises">Can I Use</a> site</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wXFoFTQf6dzGaq6fjIaWYg.png" /></figure><p>So if you are going to use the <strong><em>Promises</em></strong> for developing a web application only for new browsers you can do it without any problem, however if it is necessary to support some old browsers, use either a library with implementation of <strong><em>Promises,</em></strong> or polyfill. The libraries that implement <strong><em>Promises</em></strong> were provided earlier in the article. There are several polyfills that allow to use Promises if they are native browser objects for instance <a href="https://github.com/taylorhakes/promise-polyfill">promise-polyfill</a>. There is also exist <a href="https://babeljs.io/docs/usage/polyfill/">polyfill</a> for Babel that can be used in a project where <a href="https://babeljs.io/">Babel</a> is used.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d770bd11bf87" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>