<?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 Anand Kumar on Medium]]></title>
        <description><![CDATA[Stories by Anand Kumar on Medium]]></description>
        <link>https://medium.com/@anandkumar_16061?source=rss-91cfa8a3a640------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0364TdAq1xCw0aRp.</url>
            <title>Stories by Anand Kumar on Medium</title>
            <link>https://medium.com/@anandkumar_16061?source=rss-91cfa8a3a640------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 19:18:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anandkumar_16061/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[All about custom directives in Angular]]></title>
            <link>https://medium.com/@anandkumar_16061/how-to-use-and-create-custom-directives-in-angular-276381e5f1d0?source=rss-91cfa8a3a640------2</link>
            <guid isPermaLink="false">https://medium.com/p/276381e5f1d0</guid>
            <dc:creator><![CDATA[Anand Kumar]]></dc:creator>
            <pubDate>Wed, 29 Mar 2023 13:49:51 GMT</pubDate>
            <atom:updated>2023-03-29T14:18:08.656Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is an Angular Directive?</h3><p>In Angular, directives are used to add behavior and functionality to DOM elements. Directives are the functions which will execute whenever Angular compiler finds it<strong>.</strong> Angular Directives enhance the capability of HTML elements by attaching custom behaviors to the DOM.</p><p>From the core concept, Angular directives are categorized into three categories.</p><ol><li><strong>Attribute Directives</strong></li><li><strong>Structural Directives</strong></li><li><strong>Components</strong></li></ol><h4>Attribute Directives</h4><p>Attribute Directives are responsible for manipulating the appearance and behavior of DOM elements. We can use attribute directives to change the style of DOM elements. These directives are also used to hide or show particular DOM elements conditionally. Angular provides many built-in Attribute Directives like <strong>NgStyle</strong>, <strong>NgClass</strong>, etc. We can also create our own custom Attribute Directives for our desired functionality.</p><h4>Structural Directives</h4><p>Structural Directives are responsible for changing the structure of the DOM. They work by adding or removing the elements from the DOM, unlike Attribute Directives which just change the element’s appearance and behavior.</p><p>You can easily differentiate between the Structural and Attribute Directive by looking at the syntax. The Structural Directive’s name always starts with an asterisk(*) prefix, whereas Attribute Directive does not contain any prefix. The three most popular built-in Structural Directives Angular provides are <strong>NgIf</strong>, <strong>NgFor</strong>, and <strong>NgSwitch</strong>.</p><h4>Components</h4><p>Components are directives with templates. The only difference between Components and the other two types of directives is the Template. Attribute and Structural Directives don’t have Templates. So, we can say that the Component is a cleaner version of the Directive with a template, which is easier to use.</p><h3>Using Angular Built-In Directives</h3><p>There are a lot of built-in Directives available in Angular which you can easily use. In this section, we will use two very simple built-in directives.</p><p><strong>NgStyle Directive</strong> is an Attribute directive used to change the styling of any DOM element on the basis of some condition.</p><pre>&lt;p [ngStyle]=&quot;{&#39;background&#39;: isBlue ? &#39;blue&#39; : &#39;red&#39;}&quot;&gt; I am an Attribute Directive&lt;/p&gt;</pre><blockquote><em>In the above code snippet, we are adding a blue background if the value of </em><em>isBlue variable is true. If the value of </em><em>isBlue variable is false, then the background of the above element will be red.</em></blockquote><p><strong>NgIf Directive </strong>is a structural directive used to add elements into the DOM according to some condition.</p><pre>&lt;p *ngIf=&quot;show&quot;&gt;I am a Structural Directive&lt;/p&gt;</pre><blockquote><em>In the above code snippet, the whole paragraph will remain in the DOM if the value of </em><em>show variable is true, else it will kick off from the DOM.</em></blockquote><h3>Creating a Custom Attribute Directive</h3><p>Creating a custom directive is just like creating an Angular component. To create a custom directive we have to replace @Component decorator with @Directive decorator.</p><p>So, let’s get started with creating our first Custom Attribute directive. In this directive, we are going to highlight the selected DOM element by setting an element’s background color.</p><p>Create an app-highlight.directive.ts file in src/app folder and add the code snippet below.</p><pre>import { Directive, ElementRef } from &#39;@angular/core&#39;;<br>@Directive({<br>    selector: &#39;[appHighlight]&#39;<br>})<br>export class HighlightDirective {<br>    constructor(private eleRef: ElementRef) {<br>        eleRef.nativeElement.style.background = &#39;red&#39;;<br>    }<br>}</pre><p>Here, we are importing Directive and ElementRef from Angular core. The Directive provides the functionality of @Directive decorator in which we provide its property selector to appHighLight so that we can use this selector anywhere in the application. We are also importing the ElementRef which is responsible for accessing the DOM element.</p><p>Now to get appHighlight Directive to work, we need to add our Directive to the declarations array in the app.module.ts file.</p><pre><br>import ...;<br>import { ChangeThemeDirective } from &#39;./app-highlight.directive&#39;;<br>@NgModule({<br>    declarations: [<br>        AppComponent,<br>        ChangeThemeDirective<br>    ],<br>    imports: [<br>        ...<br>],<br>    providers: [],<br>    bootstrap: [AppComponent]<br>})<br>export class AppModule { }</pre><p>Now we are going to use our newly created custom directive. I am adding the appHightlight directive in the app.component.html but you can use it anywhere in the application.</p><pre>&lt;h1 appHightlight&gt;Highlight Me !&lt;/h1&gt;</pre><p>The output of the above code snippet will look like this.</p><h3>Creating a Custom Structural Directive</h3><p>In the previous section, we created our first Attribute directive. The same approach is used to create the structural directive as well.</p><p>So, let’s get started with creating our structural directive. In this directive, we are going to implement the *appNot directive which will work just opposite of *ngIf.</p><p>Now create a app-not.directive.ts file in the src/app folder and add the code below.</p><pre>import { Directive, Input, TemplateRef, ViewContainerRef } from &#39;@angular/core&#39;;<br>@Directive({<br>    selector: &#39;[appNot]&#39;<br>})<br>export class AppNotDirective {<br>    constructor(<br>        private templateRef: TemplateRef&lt;any&gt;,<br>        private viewContainer: ViewContainerRef) { }<br>    @Input() set appNot(condition: boolean) {<br>        if (!condition) {<br>            this.viewContainer.createEmbeddedView(this.templateRef);<br>        } else {<br>            this.viewContainer.clear();<br>        }<br>    }<br>}</pre><p>As you saw in the above code snippet, we are importing Directive, Input, TemplateRef and ViewContainerRef from @angular/core.</p><p>Directive provides the same functionality for the @Directive decorator. The Input decorator is used to communicate between the two components. It sends data from one component to the other using property binding.</p><p>TemplateRef represents the embedded template which is used to instantiate the embedded views. These embedded views are linked to the template which is to be rendered.</p><p>ViewContainerRef is a container where one or more views can be attached. We can use createEmbeddedView() function to attach the embedded templates in the container.</p><p>Now to get the appNot directive to work, we need to add our directive to the declarations array in the app.module.ts file.</p><pre>import ...;<br>import { AppNotDirective } from &#39;./app-not.directive&#39;;<br>@NgModule({<br>    declarations: [<br>        AppComponent,<br>        AppNotDirective<br>    ],<br>    imports: [<br>        ...<br>],<br>    providers: [],<br>    bootstrap: [AppComponent]<br>})<br>export class AppModule { }</pre><p>Now, it’s time to use our newly created structural directive.</p><p>I am adding the appNot directive in the app.component.html but you can use it anywhere in the application.</p><pre>&lt;h1 *appNot=&quot;true&quot;&gt;True&lt;/h1&gt;&lt;h1 *appNot=&quot;false&quot;&gt;False&lt;/h1&gt;</pre><p>The *appNot directive is designed in a way that it appends the template element into the DOM if the *appNot value is false just opposite the *ngIf directive.</p><p>The output of the above code snippet will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/702/0*h-xWTBj9dSWtIa8P" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=276381e5f1d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All About Promises]]></title>
            <link>https://medium.com/@anandkumar_16061/all-about-promises-f3f090eb47b3?source=rss-91cfa8a3a640------2</link>
            <guid isPermaLink="false">https://medium.com/p/f3f090eb47b3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[promises]]></category>
            <dc:creator><![CDATA[Anand Kumar]]></dc:creator>
            <pubDate>Wed, 09 Mar 2022 08:14:48 GMT</pubDate>
            <atom:updated>2022-03-09T08:47:01.935Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>In this post we will discuss about Promise.any vs Promise.race, Promise.all vs Promise.allSettled, about their definition and uses.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hW2Ajhd486zT7Df4" /><figcaption>Photo by <a href="https://unsplash.com/@mmayyer?utm_source=medium&amp;utm_medium=referral">marcos mayer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Promise.any<br></strong>Promise.any() takes an iterable of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfill (if all of the given promises are rejected), then the returned promise is rejected with an <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError">AggregateError</a></p><p>Let&#39;s understand the definition by an example below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fanand1786-iec%2Fembed%2Fpreview%2FXWzLNyJ%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DXWzLNyJ&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fanand1786-iec%2Fpen%2FXWzLNyJ&amp;image=https%3A%2F%2Fshots.codepen.io%2Fanand1786-iec%2Fpen%2FXWzLNyJ-512.jpg%3Fversion%3D1646815477&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/46685708cf15d1d399ff197004b4ee09/href">https://medium.com/media/46685708cf15d1d399ff197004b4ee09/href</a></iframe><p>click on the result and observe that we have result as “I am resolved1”. So, we can understand that Promise.any will return as soon as one of the promise is resolved.</p><p>Let’s understand what happen when all of the Promises get’s rejected:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fembed%2Fpreview%2FyLJmGZN%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DyLJmGZN&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fpen%2FyLJmGZN&amp;image=https%3A%2F%2Fshots.codepen.io%2Fvinaykrmittal%2Fpen%2FyLJmGZN-512.jpg%3Fversion%3D1606195094&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ef7335fc6149b86868971b5522627b5/href">https://medium.com/media/6ef7335fc6149b86868971b5522627b5/href</a></iframe><p>As you can see above result is “AggregateError: All promises were rejected”. This is the case when all the Promises is get’s rejected. So, we can understand that Promise.any will only give error when all the promises gets rejected no one get resolved.</p><p><strong>Promise.race<br></strong>The <strong>Promise.race()</strong> method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D1YP-1P7RU7oKbO6" /><figcaption>Photo by <a href="https://unsplash.com/@marcrafanell?utm_source=medium&amp;utm_medium=referral">Marc Rafanell López</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Let’s understand this with an example below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fembed%2Fpreview%2FgOMVqME%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DgOMVqME&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fpen%2FgOMVqME&amp;image=https%3A%2F%2Fshots.codepen.io%2Fvinaykrmittal%2Fpen%2FgOMVqME-512.jpg%3Fversion%3D1606197406&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/f14a170397b4b12988c04aa71cc3dedb/href">https://medium.com/media/f14a170397b4b12988c04aa71cc3dedb/href</a></iframe><p>As we can see result is “first rejected promise” so, we can understand that Promise.race</p><p>will return first fulfill or reject promise.</p><p>So, the difference in Promise.any and Promise.race is Promise.any will return as soon as any Promise gets fulfill or all of the Promises gets resolved on the other hand Promise.race will return as soon as any Promises gets rejected or fulfill. Promise.race will not check all other Promises if any of the Promise gets reject, it will return as soon as any of the Promises either fulfill or rejected.</p><p><strong>Promise.all<br></strong>The <strong>Promise.all()</strong> method takes an iterable of promises as an input, and returns a single <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a> that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input&#39;s promises have resolved, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fembed%2Fpreview%2FBazXMqo%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DBazXMqo&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fpen%2FBazXMqo&amp;image=https%3A%2F%2Fshots.codepen.io%2Fvinaykrmittal%2Fpen%2FBazXMqo-512.jpg%3Fversion%3D1606199682&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/cd454a50c002bbb9d5f9a02c9bd43695/href">https://medium.com/media/cd454a50c002bbb9d5f9a02c9bd43695/href</a></iframe><p>As we have seen Promise.all return when all the input Promises gets resolved or as soon as one of the Promise gets rejected (we can check by uncomment 3rd promise input). And return array of promise results when all gets resolved (we can check this in console). Main purpose of using this is when we want to take some action when all the promises gets resolved like when when we want to call a api when two other api gets resolved. But in my opinion Promise.allSettled will be better for this case. let’s checked it out.</p><p><strong>Promise.allSettled<br></strong>The <strong>Promise.allSettled()</strong> method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.<br>It is typically used when you have multiple asynchronous tasks that are not dependent on one another to complete successfully, or you&#39;d always like to know the result of each promise.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fkpE0dCVpEcEkdWZ" /><figcaption>Photo by <a href="https://unsplash.com/@floschmaezz?utm_source=medium&amp;utm_medium=referral">Florian Schmetz</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fembed%2Fpreview%2FgOMVEWw%3Fdefault-tabs%3Djs%252Cresult%26height%3D600%26host%3Dhttps%253A%252F%252Fcodepen.io%26slug-hash%3DgOMVEWw&amp;display_name=CodePen&amp;url=https%3A%2F%2Fcodepen.io%2Fvinaykrmittal%2Fpen%2FgOMVEWw&amp;image=https%3A%2F%2Fshots.codepen.io%2Fvinaykrmittal%2Fpen%2FgOMVEWw-512.jpg%3Fversion%3D1606202710&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/953877d638be2a042edc308f4d5b9e68/href">https://medium.com/media/953877d638be2a042edc308f4d5b9e68/href</a></iframe><p>The main benefit of Promise.allSettled is it will return a Promise when all the Promises response with their state. A Promise can either fulfilled or rejected. So, as we can check in the result the response of Promise.allSettled is like<br>[{“status”:”fulfilled”,”value”:”second resolved promise”},{“status”:”rejected”,”reason”:”first rejected promise”}].<br> So, it will be beneficial when we want to take some action when response comes from n no of apis and Promise.allSettled gives us the result with their state. So, we can call the api’s again whose status is rejected.</p><p>Promise.allSettled will never reject as you can see we have not added catch in the above example because it will always gets resolved with the all input promises state.</p><p>Promise.all() will reject immediately upon <strong>any </strong>of the input promises rejecting. In comparison, the promise returned by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled">Promise.allSettled()</a> will wait for all input promises to complete, regardless of whether or not one rejects. Consequently, it will always return the final result of every promise and function from the input iterable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f3f090eb47b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>