<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhi Jain</title>
    <description>The latest articles on DEV Community by Abhi Jain (@abhij1607).</description>
    <link>https://dev.to/abhij1607</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F715799%2F91e16f6c-137e-4186-9a5c-c26356e16ac4.jpeg</url>
      <title>DEV Community: Abhi Jain</title>
      <link>https://dev.to/abhij1607</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhij1607"/>
    <language>en</language>
    <item>
      <title>Promise</title>
      <dc:creator>Abhi Jain</dc:creator>
      <pubDate>Thu, 09 Jun 2022 21:37:49 +0000</pubDate>
      <link>https://dev.to/abhij1607/promise-ah2</link>
      <guid>https://dev.to/abhij1607/promise-ah2</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;The Promise object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Promise has 3 states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;pending&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;fulfilled&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;rejected&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A pending promise can either be fulfilled with a value or rejected with a reason (error). When fulfilled happens, then() method is called to handle operation and if rejected happens, catch() method is called to handle operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--N0lGXjyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/promises.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--N0lGXjyf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/promises.png" alt="alt text" width="801" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constructor&lt;/strong&gt;&lt;br&gt;
The Promise constructor is primarily used to wrap functions that do not already support promises.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise = new Promise((resolve, reject) =&amp;gt; {
  // your asynchronous task
  // resolve or reject
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;resolve and reject are also functions/ Their signatures are simple: they accept a single parameter of any type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  resolve(value) // call on resolved
  reject (reason) // call on rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resolve value parameter can be another promise object, in which case the promise gets dynamically inserted into the promise chain.&lt;br&gt;
At the time when the constructor generates the new Promise object, it also generates a corresponding pair of functions for resolve and reject; these are "tethered" to the Promise object.&lt;/p&gt;

&lt;p&gt;When called via new, the Promise constructor returns a promise object. The promise object will become "resolved" when either of the functions resolve or reject are invoked. Note that if you call resolve or reject and pass another Promise object as an argument, you can say that it is "resolved", but still cannot be said to be "settled".&lt;/p&gt;

&lt;p&gt;Lets see a function that takes a URL and then returns a new promise&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myAsyncFunction(url) {
  return new Promise((resolve, reject) =&amp;gt; {
    const xhr = new XMLHttpRequest()
    xhr.open("GET", url)
    xhr.onload = () =&amp;gt; resolve(xhr.responseText)
    xhr.onerror = () =&amp;gt; reject(xhr.statusText)
    xhr.send()
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Consuming a promise&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;then() method - it accepts two callback functions: onFulfilled and onRejected.&lt;br&gt;
&lt;code&gt;promise.then(onFulfilled,onRejected);&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;catch() method - If you want to get the error only when the state of the promise is rejected, you can use this&lt;br&gt;
&lt;code&gt;promise.catch(onRejected);&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;finally() method - It lets you execute the same piece of code whether the promise is fulfilled or rejected.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example Shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const render = () =&amp;gt; {
  //...
};

getUsers()
  .then((users) =&amp;gt; {
    console.log(users);
  })
  .catch((error) =&amp;gt; {
    console.log(error);
  })
  .finally(() =&amp;gt; {
    render();
  });

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Promises chaining&lt;/strong&gt;&lt;br&gt;
then() method generates a new promise which can optionally be used for chaining&lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(() =&amp;gt; {
    resolve('foo');
  }, 300);
});

myPromise
  .then(handleResolvedA, handleRejectedA)
  .then(handleResolvedB, handleRejectedB)
  .then(handleResolvedC, handleRejectedC);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Static Methods&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Promise.all(iterable)&lt;/strong&gt;&lt;br&gt;
   It takes an iterable of promises as input and returns a single Promise that resolves to an array of the results of the input promises.&lt;br&gt;
   It resolves after all are resolved or fails as soon as any fails.&lt;br&gt;
Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(resolve, 100, 'foo');
});

Promise.all([promise1, promise2, promise3]).then((values) =&amp;gt; {
  console.log(values);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Promise.allSettled(iterable)&lt;/strong&gt;&lt;br&gt;
   It takes an iterable of promises as input and returns a single Promise that resolves to an array of the results of the input promises.&lt;br&gt;
   It resolves after all are settled (i.e. either resolved or rejected).&lt;br&gt;
Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) =&amp;gt; setTimeout(reject, 100, 'foo'));
const promises = [promise1, promise2];

**- Promise.allSettled(promises).**
  then((results) =&amp;gt; results.forEach((result) =&amp;gt; console.log(result.status)));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Promise.any(iterable)&lt;/strong&gt;&lt;br&gt;
   As soon as any promise resolves, it returns the value of that promise&lt;br&gt;
Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 500, 'slow'));

const promises = [promise1, promise2, promise3];

Promise.any(promises).then((value) =&amp;gt; console.log(value));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Promise.race(iterable)&lt;/strong&gt;&lt;br&gt;
   Wait until any of the promises is fulfilled or rejected.&lt;br&gt;
If the returned promise resolves, it is resolved with the value of the first promise in the iterable that resolved.&lt;br&gt;
If it rejects, it is rejected with the reason from the first promise that was rejected.&lt;br&gt;
Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(resolve, 500, 'one');
});

const promise2 = new Promise((resolve, reject) =&amp;gt; {
  setTimeout(resolve, 100, 'two');
});

Promise.race([promise1, promise2]).then((value) =&amp;gt; {
  console.log(value);
  // Both resolve, but promise2 is faster
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Promise.reject(reason)&lt;/strong&gt;&lt;br&gt;
   Returns a new Promise object that is rejected with the given reason.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Promise.resolve(value)&lt;/strong&gt;&lt;br&gt;
   Returns a new Promise object that is resolved with the given value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A promise is an object that encapsulates the result of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;A promise starts in the pending state and ends in either fulfilled state or rejected state.&lt;/p&gt;

&lt;p&gt;Use then() method to schedule a callback to be executed when the promise is fulfilled, and catch() method to schedule a callback to be invoked when the promise is rejected.&lt;/p&gt;

&lt;p&gt;Place the code that you want to execute in the finally() method whether the promise is fulfilled or rejected.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Event Loop in JavaScript</title>
      <dc:creator>Abhi Jain</dc:creator>
      <pubDate>Thu, 09 Jun 2022 20:34:05 +0000</pubDate>
      <link>https://dev.to/abhij1607/event-loop-in-javascript-35pk</link>
      <guid>https://dev.to/abhij1607/event-loop-in-javascript-35pk</guid>
      <description>&lt;p&gt;Event Loop is one of the most important concepts in JavaScript.&lt;/p&gt;

&lt;p&gt;JavaScript is a single-threaded language which means it has only one call stack. Therefore, it can only do one task at a time. &lt;br&gt;
This vice is a virtue in disguise. As it increased the simplicity to program in JavaScript. &lt;/p&gt;

&lt;p&gt;But what about asynchronous behaviour on the Web. Isn't JavaScript synchronous?&lt;br&gt;
Here event loop comes to play. In most browsers, every tab has a separate event loop to avoid heavy processing to block the entire browser.&lt;/p&gt;

&lt;p&gt;Ok, but what the heck is an event loop?&lt;/p&gt;

&lt;p&gt;The event loop looks something like that shown in the picture below. It has a call stack, a microtask queue, a task queue and Web APIs also have a role to play. &lt;br&gt;
Such an iteration is called a tick in the Event Loop. Each event is just a function callback.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbp0hyntoqojxzo92zl1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbp0hyntoqojxzo92zl1d.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example - when your JavaScript program makes an Ajax request to fetch some data from the server, you set up the “response” code in a function (the “callback”), and the JS Engine tells the hosting environment:&lt;br&gt;
“Hey, I’m going to suspend execution for now, but whenever you finish with that network request, and you have some data, please call this function back.”&lt;/p&gt;

&lt;p&gt;The browser is then set up to listen for the response from the network, and when it has something to return to you, it will schedule the callback function to be executed by inserting it into the event loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And what are these Web APIs?&lt;/strong&gt; &lt;br&gt;
In essence, they are threads that you can’t access, you can just make calls to them. They are the pieces of the browser in which concurrency kicks in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How setTimeout() works&lt;/strong&gt;&lt;br&gt;
 &lt;code&gt;setTimeout(() =&amp;gt; {}), 0)&lt;/code&gt; takes callback &amp;amp; timer in millisecond as an argument. It sets up a timer. When timer expires, JavaScript environment places callback to task queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Call Stack&lt;/strong&gt;&lt;br&gt;
This is where all the code is executed. It is a LIFO queue(Last In First Out). The event loop continuously checks for the call stack to see if there is a function to run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task Queue&lt;/strong&gt;&lt;br&gt;
Whenever event loops encounter a Web API like &lt;code&gt;setTimeout(() =&amp;gt; {}), 0)&lt;/code&gt;, It removes it from Call Stack and then sends the callback function after the timer to the task queue. Once the call stack gets empty, the functions in the task queue are sent to the call stack for executing&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Microtask Queue&lt;/strong&gt;&lt;br&gt;
Whenever event loops encounter a promise in call stack, It sends it to the microtask queue. Once the call stack gets empty, the functions in the microtask queue are sent to the call stack for execution. The Microtask queue has more priority than the Task queue. Therefore, the promise gets executed first then functions from Task Queue are allowed in the call stack.&lt;/p&gt;

&lt;p&gt;Example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('Hi');
setTimeout(function cb1() { 
    console.log('cb1');
}, 5000);
console.log('Bye');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's execute the code snippet and see what happens&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F972515161212596265%2F984908942419320862%2Fevent-loop.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.discordapp.com%2Fattachments%2F972515161212596265%2F984908942419320862%2Fevent-loop.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initially, the call stack, task queue &amp;amp; microtask queue are empty.&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;console.log("Hi")&lt;/code&gt; is pushed in the call stack then executed&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;setTimeout(function cb1() { 
console.log('cb1');
}, 5000);&lt;/code&gt; is executed &amp;amp; removed from call stack. The browser creates a timer as part of the Web APIs. It is going to handle the countdown for you.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log('Bye')&lt;/code&gt; is added to the Call Stack &amp;amp; executed.&lt;/li&gt;
&lt;li&gt;After 5000 ms, the timer completes and it pushes the cb1 callback to the Task Queue.&lt;/li&gt;
&lt;li&gt;Event loop checks if call stack is empty. Finding the event loop empty, it passes the cb1 callback to call stack.&lt;/li&gt;
&lt;li&gt;cb1 is executed. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The event loop is just a guardian who keeps good communication with Call Stack and Callback Queue. It checks if the call stack is free, and then let know the callback queue. Then Callback queue passes the callback function to the Call stack to be executed. When all the callback functions are executed, the call stack is out and the global execution context is free.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>All about Web events</title>
      <dc:creator>Abhi Jain</dc:creator>
      <pubDate>Thu, 09 Jun 2022 20:32:40 +0000</pubDate>
      <link>https://dev.to/abhij1607/all-about-web-events-hlp</link>
      <guid>https://dev.to/abhij1607/all-about-web-events-hlp</guid>
      <description>&lt;p&gt;Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can react to them.&lt;/p&gt;

&lt;p&gt;JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page.&lt;/p&gt;

&lt;p&gt;Events are a part of the Document Object Model (DOM) and every HTML element contains a set of events which can trigger JavaScript Code.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loading of a webpage&lt;/li&gt;
&lt;li&gt;clicking on the button&lt;/li&gt;
&lt;li&gt;hover on some content&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Event Listeners&lt;/strong&gt;&lt;br&gt;
To react to these events, event handlers/listeners are used. A block of code is run when these events are fired.&lt;/p&gt;

&lt;p&gt;The best practice for adding an event handler is the &lt;em&gt;addEventListener()&lt;/em&gt; method. It can be used in scales with growing complexity in programs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Objects&lt;/strong&gt;&lt;br&gt;
Look at the code snippet below, Most web developers might encounter the &lt;em&gt;e/evt/event&lt;/em&gt; parameter in the function. Yes, this parameter is called the &lt;em&gt;event object&lt;/em&gt;. It is automatically passed to event handlers to provide extra features and information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const btn = document.querySelector('button');

function bgChange(e) {
  const rndCol = `rgb(${random(255)}, ${random(255)}, ${random(255)})`;
  e.target.style.backgroundColor = rndCol;
  console.log(e);
}

btn.addEventListener('click', bgChange);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Understanding the Event Flow&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's find out how an event is propagated in the DOM.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Usually, an event is propagated towards the target element starting from its parents, and then it will propagate back towards its parent element.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YQYxSURq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0slgvgq4m02vpy641cd7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YQYxSURq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0slgvgq4m02vpy641cd7.jpg" alt="Image description" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deep Dive Below&lt;/strong&gt;&lt;br&gt;
There are 3 phases in JavaScript event -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Capture Phase:&lt;/strong&gt; The event is being propagated from the ancestor toward the parent. &lt;br&gt;
&lt;em&gt;Window&amp;gt;Document&amp;gt;HTMLElement&amp;gt;target's parent&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Target Phase:&lt;/strong&gt; The event has arrived at the target&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bubble Phase:&lt;/strong&gt; It is the reverse of the capture. It propagates from the target towards the ancestor until the window.&lt;br&gt;
&lt;em&gt;target's parent&amp;gt;HTMLElement&amp;gt;Document&amp;gt;Window&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The diagram below will give you a better understanding of event propagation&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--14pTUgBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jul40va3atrhytu7db5r.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--14pTUgBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jul40va3atrhytu7db5r.jpeg" alt="Image description" width="532" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lets visualize the event propagation with the help of an example&lt;/p&gt;

&lt;p&gt;If you have a button element in the order given below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Window&amp;gt;Document&amp;gt;DIV2&amp;gt;DIV1&amp;gt;button&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Event Capturing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you can see below, the click event triggered first in Window then Document then Parent then target. Propagation will stop at the target&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zvOSsH73--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AbwNxfZVJ28WSAQ5s1MCc3A.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zvOSsH73--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AbwNxfZVJ28WSAQ5s1MCc3A.gif" alt="Image description" width="880" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Events can be registered for the capture phase by passing true in addEventListener, as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("click", () =&amp;gt; {
    console.log('Window');
  },true);

document.addEventListener("click", () =&amp;gt; {
    console.log('Document');
  },true);

document.querySelector(".div2").addEventListener("click", () =&amp;gt; { 
    console.log('DIV 2');
  },true);

document.querySelector(".div1").addEventListener("click", () =&amp;gt; {
    console.log('DIV 1');
  },true);

document.querySelector("button").addEventListener("click", () =&amp;gt; {
    console.log('CLICK ME!');
  },true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Event Bubbling&lt;/strong&gt;&lt;br&gt;
You can see below, that the click event triggered first at the target and then propagated up till Window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--B99zuQuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AsfTTnB76jtG7dhfMQa0Zsg.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--B99zuQuE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AsfTTnB76jtG7dhfMQa0Zsg.gif" alt="Image description" width="880" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Events can be registered for the bubbling phase by passing false in addEventListener or don't pass anything as it is the default behaviour, as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("click", () =&amp;gt; {
    console.log('Window');
  });

document.addEventListener("click", () =&amp;gt; {
    console.log('Document');
  });

document.querySelector(".div2").addEventListener("click", () =&amp;gt; { 
    console.log('DIV 2');
  });

document.querySelector(".div1").addEventListener("click", () =&amp;gt; {
    console.log('DIV 1');
  });

document.querySelector("button").addEventListener("click", () =&amp;gt; {
    console.log('CLICK ME!');
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see, how to manipulate the phases on different objects to register for both phases&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;window.addEventListener("click", () =&amp;gt; {
    console.log('Window');
  },true); //registered for capturing

document.addEventListener("click", () =&amp;gt; {
    console.log('Document');
  }); //registered for bubbling

document.querySelector(".div2").addEventListener("click", () =&amp;gt; { 
    console.log('DIV 2');
  }); //registered for bubbling

document.querySelector(".div1").addEventListener("click", () =&amp;gt; {
    console.log('DIV 1');
  },true); //registered for capturing

document.querySelector("button").addEventListener("click", () =&amp;gt; {
    console.log('CLICK ME!');
  },true); //registered for capturing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the event will be triggered in order &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Window&amp;gt;DIV1&amp;gt;CLICK ME in the Capture phase then &lt;br&gt;
DIV2&amp;gt;Document in the Bubble phase&lt;br&gt;
Refer to the Gif below for a visualisation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sUOFdBuH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AL53X6yq5t-Nw_vl1EH9EWA.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sUOFdBuH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://miro.medium.com/max/1400/1%2AL53X6yq5t-Nw_vl1EH9EWA.gif" alt="Image description" width="880" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But what if we don't want to propagate event&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Event object has a method available on it called stopPropagation(). When invoked on the event handler, stops propagation any further up the chain.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;btn.addEventListener('click', e =&amp;gt; {
  e.stopPropagation();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Event Delegation&lt;/strong&gt;&lt;br&gt;
When we want some code to run when the user interacts with any one of a large number of child elements, we set the event listener on their parent and have events that happen on them bubble up to their parent rather than having to set the event listener on every child individually.&lt;/p&gt;

&lt;p&gt;Lets see an example-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ul id="container"&amp;gt;
  &amp;lt;li id="mobile"&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li id="cameras"&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li id="shoes"&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li id="mobile"&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, there's a parent element with id="container" and multiple child elements. Now we want to console.log(id) of child elements on click event.&lt;/p&gt;

&lt;p&gt;1st Approach that comes to mind is to addEventListener() on every child but if the list of child elements is too long then an event listener on every element might increase complexity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const container = document.querySelector('#container');

container.addEventListener('click', (e)=&amp;gt;{
  if(e.target.tagname=="LI"){
    console.log(e.target.id);
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another approach could be to add an event listener to the parent container and use event bubbling to listen to the event and trigger the function. See the implementation above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;These concepts can help you to stop any unexpected event from firing. Moreover, it can help in performance optimization and avoid bottlenecks in the application.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Single Page Application</title>
      <dc:creator>Abhi Jain</dc:creator>
      <pubDate>Wed, 29 Sep 2021 17:54:18 +0000</pubDate>
      <link>https://dev.to/abhij1607/single-page-application-1cai</link>
      <guid>https://dev.to/abhij1607/single-page-application-1cai</guid>
      <description>&lt;p&gt;A single-page app is the kind of web app that interacts with the user by dynamically rewriting new data from the server on the current page.&lt;br&gt;
The goal of such an application is the faster transition and provide a feel like a native app to the website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3IVTM3dU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj2i774ovjsxe0ph2wde.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3IVTM3dU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj2i774ovjsxe0ph2wde.png" alt="single page application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;History&lt;/strong&gt;&lt;br&gt;
The concept of such an application was discussed in early 2003 by a programming student at Cardiff University.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Approach to build SPA&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Ajax:&lt;/em&gt; It is one of the most famous techniques. It involves an asynchronous request to the server for data. With ajax, the website directly uses javascript or javascript library to manipulate dom.&lt;br&gt;
&lt;em&gt;Web-sockets:&lt;/em&gt; For real-time communication, their use is superior to ajax. It is real-time bi-directional communication technology.&lt;br&gt;
&lt;em&gt;Javascript frameworks:&lt;/em&gt; A lot of web browser javascript frameworks and libraries have adopted SPA principles. Meteor.js is a full-stack javascript framework designed exclusively for SPA. Some other frameworks and libraries are React, Vue.js, Svelte, Knockout.js, Angular.js, Ember.js, Ext.js etc.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Challenges&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Browser History:&lt;/em&gt; the model breaks the browser's design for page history navigation using back and forward buttons.&lt;br&gt;
&lt;em&gt;Analytics:&lt;/em&gt; Tools such as google analytics rely heavily on new pages loading on the browser. In SPA, after the first page load, all page changes are handled by the application internally. Therefore, analytics fail to record stuff after the page load.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Overview on MVC framework</title>
      <dc:creator>Abhi Jain</dc:creator>
      <pubDate>Wed, 29 Sep 2021 16:24:41 +0000</pubDate>
      <link>https://dev.to/abhij1607/overview-on-mvc-framework-2nea</link>
      <guid>https://dev.to/abhij1607/overview-on-mvc-framework-2nea</guid>
      <description>&lt;p&gt;MVC Stands for Model-View-Controller. It is a famous architectural pattern used to build scalable and extensible web applications. It separates an application into 3 components i.e. Model, View and Controller.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h0igSCwG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pawdj1r7gsp403yk2ne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h0igSCwG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pawdj1r7gsp403yk2ne.png" alt="architecture flow"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;The Model&lt;/strong&gt;&lt;br&gt;
Model component corresponds to all data-related logic that the user works with. It could be business-related data or application-related data.&lt;br&gt;
&lt;strong&gt;The View&lt;/strong&gt;&lt;br&gt;
The Html page which is rendered in the browser is written in this component. As the name suggests, this component deals with the view interface of the user i.e. UI.&lt;br&gt;
&lt;strong&gt;The Controller&lt;/strong&gt;&lt;br&gt;
The controller acts as neurons for view and model components. It takes the request from the user, then manipulates the data through the model, then show the result via view to the user.&lt;br&gt;
&lt;strong&gt;Evolution of MVC&lt;/strong&gt;&lt;br&gt;
It was first introduced by Trygve Reenskaug into Smalltalk-79 while visiting the Xerox Palo Alto Research Center (PARC). Its use in web apps was exploded after the introduction of NeXT web objects in 1996. Later frameworks such as Spring, Django and Rails have increased the popularity outside traditional enterprise environments.&lt;br&gt;
&lt;strong&gt;Features of MVC&lt;/strong&gt;&lt;br&gt;
It is a highly extensible, testable and pluggable framework.&lt;br&gt;
It supports URL routing for SEO friendly URL&lt;br&gt;
There is a clear separation of logic via Model, view and controller components.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
