<?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 Akash Deshmukh on Medium]]></title>
        <description><![CDATA[Stories by Akash Deshmukh on Medium]]></description>
        <link>https://medium.com/@deshmukhakash55?source=rss-d64f14d0c35d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Akash Deshmukh on Medium</title>
            <link>https://medium.com/@deshmukhakash55?source=rss-d64f14d0c35d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 09:08:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deshmukhakash55/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[What Is Zone.js and How Can I Use It?]]></title>
            <link>https://medium.com/swlh/what-is-zone-js-and-how-can-i-use-it-63ce08a55962?source=rss-d64f14d0c35d------2</link>
            <guid isPermaLink="false">https://medium.com/p/63ce08a55962</guid>
            <category><![CDATA[zonejs-in-angular]]></category>
            <category><![CDATA[zones-in-angular]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[j-zone]]></category>
            <category><![CDATA[zone]]></category>
            <dc:creator><![CDATA[Akash Deshmukh]]></dc:creator>
            <pubDate>Sun, 20 Dec 2020 11:23:50 GMT</pubDate>
            <atom:updated>2020-12-22T15:33:53.673Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJ0RN0gi0K3GDynYHW_Q5w.jpeg" /></figure><p>Let me guess, you might be here because either you are tired of seeing the errors in browser’s developer console which we really don’t understand or you are super curious about how exactly the change detection works in angular. Well, I hope after reading this article you’d not only be able to understand the zone errors but also use some awesome features that zone.js provide us.</p><p>Let’s begin by understanding how zone.js fiddles slightly with some browser (or I should actually say platform) APIs.</p><p>During initialization of any application which imports zone.js, zone.js monkey patches the list of platform APIs.</p><p>But what is monkey patching ? Javascript’s highly dynamic nature allows us to change the behaviour of any underlying browser API. We can replace the original API with completely another behaviour or we can simply decorate the existing behaviour with some more behaviour.</p><p>Let’s understand with a example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/0*P35nzSaNCYi8U0Ui" /></figure><p>Here we are storing the existing behaviour of window’s <em>setTimeout</em> in original. On the next line we redefine the behaviour of <em>setTimeout</em> where we can do anything we want and then call the original behaviour which we have stored in original and then return back the result.</p><p>During initialization zone.js does the same process along with all the browser APIs.</p><p>Let’s take the example of the <em>setTimeout</em> API. During initialization, zone.js creates a <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> using <em>setTimeout</em>’s parameter and schedules it in the current Zone (more on it later). This <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> is an object which implements the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L594"><em>Task</em></a> interface. This <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> is responsible for storing the arguments passed during the invocation of <em>setTimeout</em>, the way of scheduling and invoking the <em>setTimeout</em> (or any other async API).</p><p><a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> are classified into three types:</p><ul><li><em>MacroTask</em> — <em>setTimeout(), cleatTimeout(), setInterval()……</em></li><li><em>MicroTask — Promise.then()</em></li><li><em>EventTask — element.addEventListener()</em></li></ul><p><a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> has a member named type which can have a value of either <em>microTask</em>, <em>macroTask</em> or <em>eventTask</em>. Structure of <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> is as belows :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qxN-dF96GGev-e1_" /></figure><p>Let’s understand with example how our <em>setTimout</em> is converted to <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a>. Let’s consider the below example :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/0*E2C-b-qpqs2dzJuV" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/0*u4tlkqC8aeW7WXIh" /></figure><p>Here we have a simple <em>setTimout</em> defined with two arguments, callback function and delay before it’s execution. This task is de-sugared to build the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> as above.</p><p>zone.js schedules this <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> on current Zone, but what is Zone ? Are there multiple time-spaced copies of zones ?</p><h3>What is Zone ?</h3><p>A Zone is an execution context that persists across async tasks. In other words, all the async tasks such as <em>setTimeout</em>, <em>Promise</em>, <em>XHRs</em> etc. are executed in a particular context. In case of zone.js, all these async tasks are executed in a single context which is called Zone. But what is execution context ? Please do read this<a href="https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c"> awesome article</a> to understand about the “execution context”.</p><p>Zone takes care of scheduling, invoking and keeping track of asynchronous tasks scheduled by application. Zones are named and are responsible for executing the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a><em>s</em>. During initialisation of an application, zone.js creates a root zone named as ‘&lt;root&gt;’ which pretty much behaves the same as the platform which it runs on (browser in case of frontend applications).</p><p>Each zone is an instance of class Zone which provides us with some API which brings us to the next section.</p><h3>Zone API:</h3><p>As mentioned above, <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a> is executed in the Zone. But how does it happen ? zone.js exposes the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L716">API</a> which allows us to use some awesome functionality provided by zone.js. Each defined zone also allows us to define the set of properties in the form of key-value which can be accessed by its members functions.</p><p>More details about each API provided by Zone is mentioned below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*myOzALnzr3VGSegy" /></figure><p>So, Let’s play with some of these static APIs exposed by this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/0*K22ggcTPpcVGvw_a" /></figure><p>Ok then, Zones are just the execution context where all the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1231"><em>ZoneTask</em></a><em>’s</em> are executed using API but what can I do with it? Let’s see one by one how it can help us.</p><p><strong>1) Forking zones:</strong></p><p>Zone also provides us with the ability to create more child zones by exposing one of its API <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L802"><em>fork</em></a> allowing us to create hierarchical structure in zones which would help us to have each zone with distinct responsibilities.</p><p>Fig 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/121/0*x6EJL4GlL9nHoI1R" /></figure><p>We can create a new Zone using <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L802"><em>fork</em></a> API passing the specification for the new zone we want to create. These specifications in terms of code are equivalent to <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> class.</p><p>Let’s try creating the above hierarchy using <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L802"><em>fork</em></a> API :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/0*PH5mwkqeSwCFsId4" /></figure><p>Here, we’re getting reference to the current Zone using <em>Zone.current</em> and then creating a child zone <em>childAZone</em> using fork of current zone where we’re passing an object of <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> as a parameter. We’re again creating a new child zone of <em>childAZone</em> and are simply logging the names of it’s parent and grandparent zones.</p><p><a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> also includes other multiple members such as <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L415"><em>onFork</em></a>, <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L428"><em>onIntercept</em></a>, <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L467"><em>onScheduleTask</em></a>, <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L443"><em>OnInvoke</em></a> etc. But what are those members ? This brings me to the next possible action we can do with zones.</p><p><strong>2) Interception:</strong></p><p>Zones provide us with the ability to intercept multiple operations done to execute the tasks in the zone. One can avail these using the defining hooks available in <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> while creating the new Zone.</p><p>Detailed explanation of each hook in <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> is mentioned below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iCPbNBEuREMmDqCz" /></figure><p>Let’s try playing around with one of the Hooks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TZANGIYF-Lio07r8" /></figure><p>Here, I am creating the same hierarchy discussed in fig. 1, but while forking new zones, I have passed in one more value in <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> i.e. <em>onInvoke</em> where I am logging some messages before and after the execution of a function in each zone. Then we’re defining a function <em>helloFunction</em> and executing it in <em>childBZone</em>. This function is then intercepted by <em>childBZone</em> since it defines interception for execution of any function as defined in it’s <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L394"><em>ZoneSpec</em></a> while creating it. It prints log “intercepted in childBZone before execution” in the console and then sends this <em>helloFunction</em> to the closest parent which also has defined interception for function executions. The closest parent which also defines interception for functions here is <em>childAZone</em>. So <em>childAZone</em> also intercepts this <em>helloFunction</em> and logs “intercepted in childAZone before execution”. <em>childAZone</em> also follows the same process and sends this function to the closest parent which also defines the interception for any function. Now in this case, there exists no parent of <em>childAZone</em> which has interception defined, so it will simply execute this function. After the execution of this function, all the trailing messages in child zones would be logged in the console. So the final logs in the console is :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/0*QbJwg-5TJYeN2icT" /></figure><p>Here in each hook, you might have noticed some of the parameters being defined. Let’s go with each parameter one by one :</p><p><em>currentZone</em> — It refers to the Zone which is currently intercepting the task.</p><p><em>targetZone</em> — It refers to the Zone in which the <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L560">Task</a> was invoked.</p><p><em>task</em> — It refers to the task that’s supposed to be executed.</p><p><em>parentZoneDelegate</em> — It’s a special object <a href="https://github.com/angular/angular/blob/362f45c4bf1bb49a90b014d2053f4c4474d132c0/packages/zone.js/lib/zone.ts#L1002"><em>ZoneDelegate</em></a> which maintains the reference to the parent Zone which defines the same hook for interception.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Td_H7F59QCxUMXOF" /></figure><h3>Applications :</h3><p>Hmmm…. Now that we have learned how zones work, How can it be helpful to me? Let’s see some of the awesome applications.</p><p><strong>1) Profiling asynchronous calls :</strong></p><p>Have you ever tried finding time taken for the execution of a function which has some asynchronous calls. Let’s try one :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/0*9YGgIS3_6m1jpFni" /></figure><p>Here we’re trying to log the time taken for the execution for the function <em>sayHelloAfterDelay</em>. We might have expected the output to be some time in milliseconds to be printed after . Well, if we know how <a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ">event loop</a> works in javascript VM, we can surely come to the conclusion that above code won’t work.</p><p>But as we studied, all async tasks are executed in a zone and zone provides us ability to create a new zone and add the interception of these tasks.</p><p>You can have a look at <a href="https://github.com/angular/angular/blob/master/packages/zone.js/example/profiling.html">this</a> application mentioned in angular source.</p><p><strong>2) Decorators :</strong></p><p>Interception of the tasks allows us to decorate the execution of tasks with some more functionality.</p><p><strong>3) DOM Rendering :</strong></p><p>Remember the old days when we used to build websites using VanillaJS. We used to bind the variables using <em>innerHTML</em> on the selected DOM element and update every time the value in script changes. As this change in the bound variables occurs only on execution on asynchronous tasks, we could use zones to intercept these tasks and update the DOM bounding when the execution is completed which is precisely how front-end frameworks like Angular use the zone.js.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=63ce08a55962" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/what-is-zone-js-and-how-can-i-use-it-63ce08a55962">What Is Zone.js and How Can I Use It?</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>