<?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>Forem: Nitesh Patel</title>
    <description>The latest articles on Forem by Nitesh Patel (@niteshxp).</description>
    <link>https://forem.com/niteshxp</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%2F1185042%2F59334677-988a-4d91-8614-8d093dcf9aa6.jpg</url>
      <title>Forem: Nitesh Patel</title>
      <link>https://forem.com/niteshxp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/niteshxp"/>
    <language>en</language>
    <item>
      <title>Polyfills in JS - Interview Questions</title>
      <dc:creator>Nitesh Patel</dc:creator>
      <pubDate>Fri, 05 Jan 2024 06:00:05 +0000</pubDate>
      <link>https://forem.com/niteshxp/polyfills-in-js-interview-questions-357</link>
      <guid>https://forem.com/niteshxp/polyfills-in-js-interview-questions-357</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Here are five important polyfills in javascript. I will be covering &lt;code&gt;forEach&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;reduce&lt;/code&gt;, &lt;code&gt;flat&lt;/code&gt; in the easiest way possible.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;NOTE : Knowledge of the &lt;code&gt;This&lt;/code&gt; keyword is crucial.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;forEach&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code below is a simple example of &lt;code&gt;forEach&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]
const ans = numbers.forEach((n) =&amp;gt; console.log(n))

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

&lt;/div&gt;



&lt;p&gt;polyfill of &lt;code&gt;forEach&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]

Array.prototype.myForEach = function (callbackfn) {
    for (let index = 0; index &amp;lt; this.length; index++) {
        callbackfn(this[index], index, this)
    }
}

numbers.myForEach((n) =&amp;gt; console.log(n))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;code&gt;map&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simple example of in-built &lt;code&gt;map&lt;/code&gt;function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]
console.log(numbers.map((n) =&amp;gt; n * 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is polyfill of &lt;code&gt;map&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]

Array.prototype.myMap = function myMap(cb) {
    const output = []
    for (let i = 0; i &amp;lt; this.length; i++) {
        let a = cb(this[i])
        output.push(a)
    }
    return output
}

console.log(numbers.myMap((n) =&amp;gt; n * 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;code&gt;filter&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is &lt;code&gt;filter&lt;/code&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 numbers = [1, 2, 3, 4, 5]
console.log(numbers.filter((n) =&amp;gt; n &amp;gt; 3));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Polyfill of &lt;code&gt;filter&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]

Array.prototype.myFilter = function (cb) {
    const output = []
    for (let i = 0; i &amp;lt; this.length; i++) {
        if (cb(this[i])) {
            output.push(this[i])
        }
    }
    return output
}

console.log(numbers.myFilter((n) =&amp;gt; n &amp;gt; 4));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;code&gt;reduce&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;example of &lt;code&gt;reduce&lt;/code&gt;method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]
console.log(numbers.reduce((acc, curr) =&amp;gt; {
    return acc + curr
}, 0)
);

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

&lt;/div&gt;



&lt;p&gt;polyfill of &lt;code&gt;reduce&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, 2, 3, 4, 5]

Array.prototype.myReduce = function (cb, initialValue) {
    let acc = initialValue;
    for (let i = 0; i &amp;lt; this.length; i++) {
        acc = acc ? cb(acc, this[i]) : this[i]
    }
    return acc
}

console.log(numbers.myReduce((acc, curr) =&amp;gt; {
    return acc + curr
}, 1));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;code&gt;flat&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of &lt;code&gt;flat&lt;/code&gt;method, it takes depth as an argument. Depth refers to how deeply nested arrays within the original array should be flattened.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, [2, [3, 4]]]
console.log(numbers.flat(2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;polyfill of &lt;code&gt;flat&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const numbers = [1, [2, [3, 4]]]

Array.prototype.myflat = function (depth) {
    const output = []
    if (!Array.isArray(this)) {
        throw new Error(`${this}.flat is not a function`);
    }

    this.forEach((n) =&amp;gt; {
        if (Array.isArray(n) &amp;amp;&amp;amp; depth &amp;gt; 0) {
            output.push(...n.myflat(depth - 1))
        } else {
            output.push(n)
        }
    });
    return output
}

console.log(numbers.myflat(2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>career</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>React 80% : useState and useEffect</title>
      <dc:creator>Nitesh Patel</dc:creator>
      <pubDate>Mon, 01 Jan 2024 17:26:41 +0000</pubDate>
      <link>https://forem.com/niteshxp/usestate-and-useeffect-5a1b</link>
      <guid>https://forem.com/niteshxp/usestate-and-useeffect-5a1b</guid>
      <description>&lt;p&gt;&lt;em&gt;Let's dive into React hooks. One thing to keep in mind is that hooks were introduced for functional components only. Hooks don't work inside classes.&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks are just functions responsible for certain behavior. In this article, we will be discussing useState and useEffect.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;useState&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;useState&lt;/em&gt; is a React hook used for managing state variables. We can declare a state variable and update it directly. useState is imported from the react module. &lt;/p&gt;

&lt;p&gt;Here is an example of useState:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [count,setCount] = useState(0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;count&lt;/code&gt;is the name of the variable, and &lt;code&gt;setCount&lt;/code&gt;is the name of the callback function that will update the state. &lt;code&gt;useState(0)&lt;/code&gt; means we set 0 as the default value. 0 will be rendered initially, and after that, when we update the variable, the new value will be rendered.&lt;/p&gt;

&lt;p&gt;Now let's take an example of a counter app to learn more&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);
  const increase = () =&amp;gt; {
    setCount(count + 1);
  };
  const decrease = () =&amp;gt; {
    setCount(count - 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;count : {count}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={decrease}&amp;gt;-&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={increase}&amp;gt;+&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we use &lt;code&gt;setCount&lt;/code&gt;to update a new value, i.e., increasing the count by 1. Similarly, we can decrease the count value by 1. This is a basic way of writing code.&lt;/p&gt;

&lt;p&gt;The problem arises when we use another &lt;code&gt;setCount&lt;/code&gt;in the increase or decrease function. The count value ideally should decrease by 2, but this won't happen.&lt;/p&gt;

&lt;p&gt;Add this code to the &lt;em&gt;counter app&lt;/em&gt;, and you will see that the count will increase and decrease by 1 only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const increase = () =&amp;gt; {
    setCount(count + 1);
    setCount(count + 1);
  };
  const decrease = () =&amp;gt; {
    setCount(count - 1);
    setCount(count - 1);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to increase or decrease the count by 2, then we should pass a callback function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const increase = () =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount + 1);
    setCount((prevCount) =&amp;gt; prevCount + 1);
  };
  const decrease = () =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount - 1);
    setCount((prevCount) =&amp;gt; prevCount - 1);
  };

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

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;setCount&lt;/code&gt;takes a callback function that will track the previous value of &lt;code&gt;count&lt;/code&gt;and then increase or decrease the count value.&lt;/p&gt;

&lt;p&gt;Note: We passed &lt;code&gt;0&lt;/code&gt;as a default value. Since &lt;code&gt;0&lt;/code&gt;is a hard-coded value, it will run every time the component renders. To avoid rendering &lt;code&gt;0&lt;/code&gt;every time, pass a callback function that will return &lt;code&gt;0&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";
function value() {
  console.log("render");
  return 0;
}
export default function App() {
  const [count, setCount] = useState(value());
  const increase = () =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount + 1);
    setCount((prevCount) =&amp;gt; prevCount + 1);
  };
  const decrease = () =&amp;gt; {
    setCount((prevCount) =&amp;gt; prevCount - 1);
    setCount((prevCount) =&amp;gt; prevCount - 1);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;count : {count}&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={decrease}&amp;gt;-&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={increase}&amp;gt;+&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;useState(value())&lt;/code&gt; and &lt;code&gt;useState(0)&lt;/code&gt; both work in the same way. Here, the value function will return &lt;code&gt;0&lt;/code&gt; and log "render" every time. To avoid calling "render" every time, write &lt;code&gt;useState(() =&amp;gt; return 0)&lt;/code&gt;. It will render only one time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;useEffect&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;useEffect&lt;/em&gt; is used to perform side effects in components and is imported from the React module. By "side effects," we mean tasks such as fetching data or updating the DOM, etc.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;useEffect&lt;/em&gt; takes two arguments: a &lt;em&gt;function&lt;/em&gt; and optional &lt;em&gt;dependencies&lt;/em&gt; (an array).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from 'react';

useEffect(() =&amp;gt; {
  //code
}, [dependencies]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The dependencies array ensures when the effect will re-run.&lt;/p&gt;

&lt;p&gt;There are three cases of dependency arrays:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No dependency array : &lt;code&gt;useEffect&lt;/code&gt; will be called every time. Let's see &lt;code&gt;useEffect&lt;/code&gt; in action:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      setCount((count) =&amp;gt; count + 1);
      console.log("called");
    }, 2000);
  });

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Count is called {count} times.&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Here, after every 2 seconds, the &lt;code&gt;count&lt;/code&gt; will increase by 1, and "called" will be logged in the console.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Empty dependency array [] : &lt;code&gt;useEffect&lt;/code&gt; will be called once,during initial render.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      setCount((count) =&amp;gt; count + 1);
      console.log("called");
    }, 2000);
  }, []);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Count is called {count} times.&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In the above code , we added an empty dependency array &lt;code&gt;[]&lt;/code&gt;.Now, &lt;code&gt;useEffect&lt;/code&gt; will run only one time.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Some value passed in the dependency array: &lt;code&gt;useEffect&lt;/code&gt; will be called every time whenever the value passed in the dependency array changes. We can pass any number of values in the dependency array.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    setTimeout(() =&amp;gt; {
      setCount((count) =&amp;gt; count + 1);
      console.log("called");
    }, 2000);
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Count is called {count} times.&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In the above code, &lt;code&gt;count&lt;/code&gt; is passed in the dependency array. So whenever &lt;code&gt;count&lt;/code&gt; is updated, &lt;code&gt;useEffect&lt;/code&gt; will be called again. In the above code, the &lt;code&gt;count&lt;/code&gt; will increase infinitely as every time &lt;code&gt;count&lt;/code&gt; is updated, &lt;code&gt;useEffect&lt;/code&gt; will be called again, and this will go on and on.&lt;/p&gt;

&lt;p&gt;There is an effect &lt;em&gt;cleanup&lt;/em&gt; function, which will stop the &lt;code&gt;useEffect&lt;/code&gt; re-execution, as there may be a memory leak problem. The &lt;em&gt;cleanup&lt;/em&gt; ensures that no unexpected behaviour occurs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    const timer = setTimeout(() =&amp;gt; {
      setCount((count) =&amp;gt; count + 1);
      console.log("called");
    }, 2000);

    return () =&amp;gt; clearInterval(timer);
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Count is called {count} times.&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;useEffect&lt;/code&gt; will at least run once in the initial render.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>hooks</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Promise APIs with Example🤝</title>
      <dc:creator>Nitesh Patel</dc:creator>
      <pubDate>Tue, 31 Oct 2023 16:32:38 +0000</pubDate>
      <link>https://forem.com/niteshxp/promise-apis-with-example-1gci</link>
      <guid>https://forem.com/niteshxp/promise-apis-with-example-1gci</guid>
      <description>&lt;p&gt;&lt;em&gt;We know that promise is a special JavaScript object. It is a placeholder for a value which can be either fulfilled or rejected.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this we will discuss how we can handle multiple promises.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; &lt;strong&gt;Promises.all&lt;/strong&gt;  - It is used to handle multiple promises together. It takes array of promises as an input.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE : You can take any iterable as input but as of now we are considering only array.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Example: &lt;em&gt;promises.all([p1,p2,p3])&lt;/em&gt; , where p1,p2,p3 are three different promises.&lt;br&gt;
Suppose p1 takes 3sec , p2 takes 1sec and p3 takes 2sec time to complete.&lt;/p&gt;

&lt;p&gt;case 1 :  If all of three promises are fulfilled/success/resolve then output will be array of resolve value of all the three individual promises.&lt;/p&gt;

&lt;p&gt;output : [val1,val2,val3]&lt;br&gt;
          where (val1,val2,val3 are object)&lt;/p&gt;

&lt;p&gt;When the code executed, all the three promises will run parallelly but wait for all to finish. In our example , after 3sec output will be generated as p1 takes maximum time(3sec).&lt;/p&gt;

&lt;p&gt;Case 2 : If any or all of the promises get rejected.&lt;/p&gt;

&lt;p&gt;When code is executed and all promises run parallelly and as soon as any of promises get rejected, error will be thrown. The error will be same as error of that particular promise.&lt;/p&gt;

&lt;p&gt;Suppose p2 get rejected while p1, p3 get resolved in the above example, then error will be thrown immediately after p2 is executed i.e. after 1sec.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Promise.allSettled&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;We have seen that if any of the promises get rejected then error is thrown, if we want result of other two promises whichever are resolved then we can use promise.allSettled&lt;/p&gt;

&lt;p&gt;Example: &lt;em&gt;promise.allSettled([p1,p2,p3])&lt;/em&gt; , where p1,p2,p3 are three different promises.&lt;br&gt;
Suppose p1 takes 3sec , p2 takes 1sec and p3 takes 2sec time to complete.&lt;/p&gt;

&lt;p&gt;case 1 : If all promises get resolve then output will be array of resolve value of promise.&lt;/p&gt;

&lt;p&gt;case2 : If any of promise get rejected.&lt;/p&gt;

&lt;p&gt;Suppose p2 fails/rejected and p1, p3 get resolved then output will be array of return value of each promises. It doesn't matter if return value is error or success and it will wait for all the promises to settle.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE : Promise.all is like fast failing and Promise.allSettled wait for settling promise then give result accordingly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Promise.race&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this whichever promise get settled first, whether it is resolved or rejected, output is given accordingly. It doesn't wait for other promises to settle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Promise.any&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is same as promise.race but the difference is that it will wait for first fullfilled/resolved/success promise result and then output is produced.&lt;/p&gt;

&lt;p&gt;If all of the promises fails/rejected then output will be aggregate error.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promise</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Understand CORS 🌐 in Easy Way🦾</title>
      <dc:creator>Nitesh Patel</dc:creator>
      <pubDate>Sun, 15 Oct 2023 07:00:12 +0000</pubDate>
      <link>https://forem.com/niteshxp/understand-cors-in-easy-way-36ic</link>
      <guid>https://forem.com/niteshxp/understand-cors-in-easy-way-36ic</guid>
      <description>&lt;p&gt;&lt;strong&gt;CORS : Cross-Origin Resource Sharing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Basically CORS is a security feature of web browser that allow or restrict the web page in one domain to access resource from other domain (cross-origin).&lt;/p&gt;

&lt;p&gt;For example, when you fetch data from an api, an error occurred. You may have seen the error in the console of the browser. It happens because api doesn't know that the request you're sending is secure or not.&lt;br&gt;
To access the resource, browser need to allow cross-origin request.&lt;/p&gt;

&lt;p&gt;Here CORS mechanism comes into picture.&lt;/p&gt;

&lt;p&gt;The response we get after sending a request for data from api, contains HTTP header also. This HTTP header has &lt;em&gt;Access-control-allow-origin&lt;/em&gt; and &lt;em&gt;Access-control-allow-method&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Preflight Request&lt;/em&gt;&lt;/strong&gt; is sent automatically from browser as a part of CORS mechanism. It is used to check whether the cross-origin(other domain) is safe to execute or not.&lt;/p&gt;

&lt;p&gt;After preflight request, actual request is sent to the other domain i.e. cross-origin.&lt;/p&gt;

&lt;p&gt;Not every time preflight request is sent.For simple request, browser doesn't sent preflight request.&lt;br&gt;
It is only sent if browser detect that there might be some security concerns.&lt;/p&gt;

&lt;p&gt;That's the simple explaination of cors. For in-depth information, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;MDN&lt;/a&gt; docs are best! &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>cors</category>
      <category>security</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
