<?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: Pritam Kumar</title>
    <description>The latest articles on Forem by Pritam Kumar (@pritamkr_63).</description>
    <link>https://forem.com/pritamkr_63</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%2F667755%2F29671e9b-9239-4446-b2af-d0527d470709.png</url>
      <title>Forem: Pritam Kumar</title>
      <link>https://forem.com/pritamkr_63</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pritamkr_63"/>
    <language>en</language>
    <item>
      <title>Closures in JavaScirpt</title>
      <dc:creator>Pritam Kumar</dc:creator>
      <pubDate>Thu, 19 May 2022 07:27:21 +0000</pubDate>
      <link>https://forem.com/pritamkr_63/closures-in-javascirpt-dd6</link>
      <guid>https://forem.com/pritamkr_63/closures-in-javascirpt-dd6</guid>
      <description>&lt;p&gt;Hello, Everyone! Hope You all are doing well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Table of Contents
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What is Scope?&lt;/li&gt;
&lt;li&gt;What is Global, Block and Function scope?&lt;/li&gt;
&lt;li&gt;What is Lexical Scope?&lt;/li&gt;
&lt;li&gt;What is Closures?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this Blog, I'll be writing about Closures, one of the confusing topic in JavaScript. And I'm sure that every JavaScript programmer encountered this name while learning JavaScript Language.&lt;/p&gt;

&lt;p&gt;To understand Closures in better way. Here's is prerequisites to know. But don't worry if you don't know these concepts, I'm also writing about these Topics Such as Scope, Block scope, Function scope, Lexical Scope as mentioned above. &lt;/p&gt;

&lt;p&gt;Let's Start with Scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope
&lt;/h3&gt;

&lt;p&gt;The simple meaning of Scope is accessibility of variable from where is defined in JavaScript program.&lt;/p&gt;

&lt;p&gt;Let's Understand with example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; let a = "JavaScript";
 console.log(a); // JavaScirpt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, It can be easily accessible. That's ok&lt;br&gt;
But If we declare a variable inside a if block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
  let a = 'JavaScript';
}

console.log(a); //  ReferenceError: a is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example will throw an ReferenceError: a is not defined, Because, if code block created scope with variable that only accessible inside that block only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Global Scope
&lt;/h3&gt;

&lt;p&gt;If The variable declared outside of any other scope, and that variable can be access to any other scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var a = 5;
var b = 5;

function sum() {
  if (true) {
    const sum = a + b;
    console.log(sum); // 10
  }
}
sum();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, I declare two variable a, b and assign a value 5. The global variable accessed inside other scope, That's why it logs sum = 10&lt;/p&gt;

&lt;h3&gt;
  
  
  Block Scope
&lt;/h3&gt;

&lt;p&gt;When ES6 Introduced, Two new keyword were introduced to declare variable in JavaScirpt let and const, Both are block scope keyword. That can be only access inside that block only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
  const message = 'Hello JavaScript';
  console.log(message); // 'Hello JavaScript'
}
console.log(message); // ReferenceError
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the first log, It prints Hello JavaScript, because we are accessing inside that block but if we try to access outside of block it will throw an ReferenceError.&lt;/p&gt;

&lt;p&gt;Note:- The code block for, while also create block scope.&lt;/p&gt;

&lt;p&gt;Note: - var is not block scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
  var message = 'Hello JavaScript';
}
console.log(message); 'Hello JavaScript'

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

&lt;/div&gt;



&lt;p&gt;In case of var keyword, if {} code block does not create a scope. That's why we're accessed message variable outside of the if block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Scope
&lt;/h3&gt;

&lt;p&gt;A JavaScript functions create scope with all variables (let, const, and var) that are only accessible inside that function body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sum() {
  var a = 5;
  let b = 5;
  const c = 5;
}
sum();

console.log(a, b, c); // ReferenceError: a is not defined

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lexical Scope
&lt;/h3&gt;

&lt;p&gt;Let's start with 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;let myName = 'Pritam';

function one() {
  let age = 15;

  function two() {
    let address = 'Hazaribagh';

    let bio = `My name is ${myName}. and age is ${age}, and Address is ${address}`;

    console.log(bio); // My name is Pritam. and age is 15, and Address is Hazaribagh

  }

  two();
}

one();

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

&lt;/div&gt;



&lt;p&gt;In the Above Example&lt;/p&gt;

&lt;p&gt;The function two() can access of it's Parent scopes variables, &lt;br&gt;
also can access variable of global scope. But How? How a function scope has access of it's outer function scope. Let's know about it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The answer is Because of Lexical scope / Lexical Environment&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the concept of Lexical scope, inner function has access of outer function's scopes.&lt;/p&gt;

&lt;p&gt;But How it works? , How it remembers?&lt;/p&gt;

&lt;p&gt;In JavaScript, Whenever a function created, It make reference of it's lexical variable due to an internal hidden property &lt;br&gt;
[[Environment]].&lt;/p&gt;

&lt;p&gt;Note: All function has [[Environment]] property.&lt;/p&gt;
&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;A Closures is the combination of function bundle together with reference of It's lexical environment.&lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;A Closures allows you access of lexical scope even outside of it's lexical scope.&lt;/p&gt;

&lt;p&gt;Note: - Every time Closures form when function created.&lt;/p&gt;

&lt;p&gt;Let's Understand with 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;function outerFunc() {
  let outerVar = 'I am Developer!';
  function innerFunc() {
    console.log(outerVar); // =&amp;gt; logs "I am outside!"
  }
  innerFunc();
}

outerFunc();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, due to lexical scope, innerFunc() has access of It's lexical variable. That we know very well.&lt;/p&gt;

&lt;p&gt;Now let's make changes on program, innerFunc() invoked to be outside of outerFunc() scope or lexical scope. I mean, in other function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function outerFunc() {
  let outerVar = 'I am Developer!';
  function innerFunc() {
    console.log(outerVar); // =&amp;gt; logs "I am outside!"
  }
 return innerFunc
}

function newFunc() {
  const myInnerFunc = outerFunc();
  myInnerFunc();
}
 newFunc()

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

&lt;/div&gt;



&lt;p&gt;In this case innerFunc has still access of its lexical scope variable (outerVar), even executed outside the scope of outerFunc(). This is case, closure comes into picture. Due to closure the innerFunc remembers it's lexical scope variable from the place where it is defined. No matter, the innerFunc invoked outside of the scope.&lt;/p&gt;

&lt;p&gt;I hope this will give you an basic understanding of Closures.&lt;/p&gt;

&lt;h4&gt;
  
  
  References:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dmitripavlutin.com/simple-explanation-of-javascript-closures/"&gt;https://dmitripavlutin.com/simple-explanation-of-javascript-closures/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/51748127/what-is-the-javascript-environment-property"&gt;https://stackoverflow.com/questions/51748127/what-is-the-javascript-environment-property&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>PollyFill for map(), filter(), forEach().</title>
      <dc:creator>Pritam Kumar</dc:creator>
      <pubDate>Sat, 14 May 2022 08:00:18 +0000</pubDate>
      <link>https://forem.com/pritamkr_63/pollyfill-for-map-filter-foreach-27k4</link>
      <guid>https://forem.com/pritamkr_63/pollyfill-for-map-filter-foreach-27k4</guid>
      <description>&lt;p&gt;Hello Everyone! Hope you all are doing great.&lt;/p&gt;

&lt;h4&gt;
  
  
  Table of contents
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Function?

&lt;ul&gt;
&lt;li&gt;Ways to writing a function in JavaScript?&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Higher order function?&lt;/li&gt;
&lt;li&gt;First order function?&lt;/li&gt;
&lt;li&gt;What is pollyfill?&lt;/li&gt;
&lt;li&gt;Pollyfill for map, filter, forEach&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before Writing about pollyfill, I'll explain about function, Higher order function, and First order function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0pJO_v_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f190vbxexf0hotaftsj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0pJO_v_v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5f190vbxexf0hotaftsj.gif" alt="Image description" width="480" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's dive into it...&lt;/p&gt;

&lt;h3&gt;
  
  
  What are function in JavaScript?
&lt;/h3&gt;

&lt;p&gt;Function in JavaScript are Building block of code (Statement).&lt;br&gt;
Which is responsible of a particular operations, that can be re-use in your JavaScript program without repetition. (You have to just invoke it, where you want to use this function)&lt;/p&gt;

&lt;p&gt;It takes some value as input(argument),and return some value after invocation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I850rJbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/958re1dlgcfbgjl9yfxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I850rJbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/958re1dlgcfbgjl9yfxb.png" alt="Image description" width="751" height="259"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  There are Three ways to write function in JavaScript?
&lt;/h4&gt;
&lt;h4&gt;
  
  
  Function Declaration
&lt;/h4&gt;

&lt;p&gt;It is a Traditional ways to declare a function in JavaScript. It start with function keyword, after that function name with parenthesis and then curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Function declaration

function add(x, y) {        
    console.log(x + y);
}

// Function invocation 
add(2, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Function Expression
&lt;/h4&gt;

&lt;p&gt;Function are first class citizen in JavaScript. &lt;br&gt;
In JavaScript function are treated as like any other variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Single line of code
let add = (x, y) =&amp;gt; x + y;

console.log(add(3, 2));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Function Expression, If there are more than one parameter in function should use parentheses with parameter. And when there are multiple lines on code we should use return keyword in the expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Multiple line of code

const great = (x, y) =&amp;gt; {
  if (x &amp;gt; y) {
    return 'a is greater';
  } else {
    return 'b is greater';
  }
};

console.log(great(3, 5));

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

&lt;/div&gt;



&lt;p&gt;Now Let's Know about Higher order Function and First order function&lt;/p&gt;

&lt;p&gt;In JavaScript Higher order function either it take function as argument or return a function are know as Higher order function.&lt;br&gt;
On the other hand, If a function that takes only primitives or object as arguments and returns only primitive or object are know as First order function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//returning a function
function nums(x, y) {
  return function sum() {
    return `The sum is ${x + y}`;
  };
}

const getSum = nums(3, 5);

console.log(getSum());

//function as a parameter
function message (name) {
  console.log(`Hello ${name}`)
}

function greet (name, cb) {
  cb(name)
}

greet("Pritam", message)

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

&lt;/div&gt;



&lt;p&gt;Functions such as filter(),map(),reduce(), forEach etc, these all are example of Higher-Order Functions in JavaScript. which is use with a array.&lt;/p&gt;

&lt;p&gt;Now Let's move to pollyfill.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pollyfill
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A Pollyfill is a piece of code used to provide modern functionality to older browser that do not natively .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suppose a older browser does not support modern functionality, A developer code that functionality from scratch. &lt;/p&gt;

&lt;p&gt;It is Browser fallback that it does not support.&lt;/p&gt;

&lt;p&gt;Let's write first pollyfill for map() function.&lt;br&gt;
Before that, knows about what it does with an Array.&lt;/p&gt;
&lt;h3&gt;
  
  
  map()
&lt;/h3&gt;

&lt;p&gt;The map() function/method call a callback function once for each element in the array. &lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;The map() method to iterate over an array and modifies the array element using a callback function. and return a new array. does not mutate the original array.&lt;/p&gt;

&lt;p&gt;The callback function in map has three arguments current Element, index and original array.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [3, 6, 9, 1, 2];
const newArray = array.map((num) =&amp;gt; num * 3);
console.log(newArray);

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

&lt;/div&gt;

&lt;h4&gt;
  
  
  Pollyfill for map()
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myMap = function (callback) {
  let newArray = [];

  for (let i = 0; i &amp;lt; this.length; i++) {
    newArray.push(callback(this[i], i, this));
  }

  return newArray

};

const result = array.map((num) =&amp;gt; num * 3);
console.log(result);

// Here, this keyword reference to parent array.

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

&lt;/div&gt;

&lt;h3&gt;
  
  
  forEach()
&lt;/h3&gt;

&lt;p&gt;The forEach method call a callback function once for each element in the array. It does not return new array. Mostly is uses over for loop, just to iterate an array element.&lt;br&gt;
&lt;/p&gt;

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

array.forEach((num) =&amp;gt; {
  console.log(num); // 3, 6, 9, 1, 2

});

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pollyfill for forEach()
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myForEach = function (callback) {
//Since forEach does not return array
  for (let i = 0; i &amp;lt; this.length; i++) {
    callback(this[i], i, this); 
  }
};

array.myForEach((num) =&amp;gt; console.log(num));

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  filter()
&lt;/h3&gt;

&lt;p&gt;The filter method return a new array with all elements that pass the test implemented by provider function. It does not mutate the &lt;br&gt;
original array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const array = [3, 6, 9, 1, 2];
const result = array.filter((num) =&amp;gt; num&amp;gt; 3)
console.log(result) // [6, 9]

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Pollyfill for filter
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myFilter = function (callback) {
  let newArray = [];

  for (let i = 0; i &amp;lt; this.length; i++) {
    if (callback(this[i], i, this)) {
      newArray.push(this[i]);
    }
  }

  return newArray;
};

const result = array.myFilter((num) =&amp;gt; num &amp;gt; 3);
console.log(result); // [6, 9]

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

&lt;/div&gt;



&lt;p&gt;What is Array.prototype?&lt;/p&gt;

&lt;p&gt;The JavaScript array prototype constructor is used to allow to add new methods and properties to the Array() object.&lt;/p&gt;

&lt;p&gt;Thank you for reading...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Redux Toolkit: createAsyncThunk</title>
      <dc:creator>Pritam Kumar</dc:creator>
      <pubDate>Fri, 13 May 2022 08:03:05 +0000</pubDate>
      <link>https://forem.com/pritamkr_63/redux-toolkit-createasyncthunk-2792</link>
      <guid>https://forem.com/pritamkr_63/redux-toolkit-createasyncthunk-2792</guid>
      <description>&lt;h3&gt;
  
  
  Table of Content
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Redux&lt;/li&gt;
&lt;li&gt;Redux Toolkit&lt;/li&gt;
&lt;li&gt;createAsyncThunk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hello Everyone! Hope you all are doing great. 😊&lt;/p&gt;

&lt;p&gt;In this Blog I'm writing about createAsyncThunk but before diving into this I'll explain Redux and Redux toolkit.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is &lt;a href="https://blog.logrocket.com/why-use-redux-reasons-with-clear-examples-d21bffd5835/"&gt;Redux&lt;/a&gt;?
&lt;/h3&gt;

&lt;p&gt;Redux is open source JavaScript library to managing state in JavaScript applications runs in different platform such as client side, server side. Redux commonly use with JavaScript libraries such as Reactjs, Angular etc. With redux all states of a application states kept in a store, which can be share to all component of your application. &lt;/p&gt;

&lt;h4&gt;
  
  
  When use Redux ?
&lt;/h4&gt;

&lt;p&gt;In general, When your application grows at large scale, where managing states is difficult to you. Now you can look for tools like Redux. It's makes easy to managing states at one place. &lt;/p&gt;

&lt;h4&gt;
  
  
  Problem with Redux ?
&lt;/h4&gt;

&lt;p&gt;According to official docs, There are three major problem with That developers are facing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Configuring a Redux store is too complicated”&lt;/li&gt;
&lt;li&gt;“I have to add a lot of packages to get Redux to do anything useful”&lt;/li&gt;
&lt;li&gt;“Redux requires too much boilerplate code”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now Let's Understand Redux Toolkit&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Redux Toolkit?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Redux Toolkit is abstraction version of Redux.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redux toolkit is the new standard way to write redux logic which is solve our three major problem, which is mentioned above with Redux. &lt;/p&gt;

&lt;h4&gt;
  
  
  Features comes with Redux Toolkit
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;configureStore()&lt;/li&gt;
&lt;li&gt;createReducer()&lt;/li&gt;
&lt;li&gt;createAction()&lt;/li&gt;
&lt;li&gt;createSlice()&lt;/li&gt;
&lt;li&gt;createAsyncThunk()&lt;/li&gt;
&lt;li&gt;createEntityAdapter()&lt;/li&gt;
&lt;li&gt;createSelector()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can read more about &lt;a href="https://redux-toolkit.js.org/introduction/getting-started"&gt;Redux Toolkit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now You have a basic knowledge of Redux and Redux Toolkit. Let's understand createAsyncThunk()&lt;/p&gt;

&lt;h3&gt;
  
  
  What is createAsyncThunk() ?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;Redux Thunk is middleware that allows you to return functions, rather than just actions, within Redux. This allows for delayed actions, including working with promises. &lt;a href="https://www.freecodecamp.org/news/redux-thunk-explained-with-examples/"&gt;link&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to the official docs&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This abstracts the standard recommended approach for handling async request lifecycles.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;createAsyncThunk is a middleware to perform asynchronous operations such as fetching an API, This package included by Default with Redux Toolkit. &lt;br&gt;
                          Basically, createAsyncThunk() is function which is take three parameter&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;type&lt;/li&gt;
&lt;li&gt;payloadCreator&lt;/li&gt;
&lt;li&gt;options&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's understand one by one&lt;/p&gt;
&lt;h3&gt;
  
  
  type:
&lt;/h3&gt;

&lt;p&gt;"data/getData" (reducerName/actionType)&lt;/p&gt;

&lt;p&gt;A string value generate a constant action type. It represent the life cycle of a async operations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pending: 'data/getData/pending'&lt;/li&gt;
&lt;li&gt;fulfilled: 'data/getData/fulfilled'&lt;/li&gt;
&lt;li&gt;rejected: 'data/getData/rejected'&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  payloadCreator
&lt;/h3&gt;

&lt;p&gt;A function with two parameter &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;arg&lt;/li&gt;
&lt;li&gt;thunkAPI&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  arg
&lt;/h4&gt;

&lt;p&gt;A single value that can be passed to the thunk creator, when createAsyncThunk function dispatched. (we'll see through an example.)&lt;/p&gt;
&lt;h4&gt;
  
  
  thunkAPI
&lt;/h4&gt;

&lt;p&gt;ThunkAPI is an object, contains all the parameter that can be passed to redux thunk function.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;dispatch: for dispatching different actions.&lt;/li&gt;
&lt;li&gt;getState: to access the redux store from within the callback&lt;/li&gt;
&lt;li&gt;requestId: this is a unique id redux-toolkit generates for each request&lt;/li&gt;
&lt;li&gt;signal: this can be used to cancel request.&lt;/li&gt;
&lt;li&gt;rejectWithValue: It is a utility function that can returns to the action creator a defined payload, in case of error.&lt;/li&gt;
&lt;li&gt;extra: the “extra argument” given to the thunk middleware on setup, if available&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Options
&lt;/h4&gt;

&lt;p&gt;It is an object with options field.&lt;/p&gt;

&lt;p&gt;The main reason to use createAsyncThunk(). It generates promise life cycle action types based on three states.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pending: 'data/getData/pending'&lt;/li&gt;
&lt;li&gt;fulfilled: 'data/getData/fulfilled'&lt;/li&gt;
&lt;li&gt;rejected: 'data/getData/rejected'&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Let's Examine with an example
&lt;/h3&gt;

&lt;p&gt;Step1: &lt;/p&gt;

&lt;p&gt;We have to create a slice and make a API fetch call with createAsyncThunk function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createAsyncThunk } from '@reduxjs/toolkit';

export const getAllData = createAsyncThunk(
  'data/getData',
  async () =&amp;gt; {
    return axios.get(`https://jsonplaceholder.typicode.com/posts`);
  }
);

const initialState = {
    loading: false,
    data: [],
    error: ""
}

const dataSlice = createSlice({
    name: 'data',
    initialState,
    extraReducers: {
      [getAllData.pending] = (state) =&amp;gt; {
        state.loading = true;
      },

      [getAllData.fulfilled] = (state, action) =&amp;gt; {
        state.loading = 'Fulfilled';
        state.data = action.payload;
      },

      [getAllData.rejected] = (state) =&amp;gt; {
        state.loading = false;
        state.error = "Error occurred"
      }
    }

export default dataSlice.reducer;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;On the Initial Call of the createAsyncThunk dispatch data/getData/pending lifecycle action type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If our data fetch is successful, the data/getData/pending action type gets dispatched.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In case of data/getData/rejected is dispatched, then createAsyncThunk function return a rejected promise containing with an error. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In this extraReducer, we make our changes to the states&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hope This blog will give basic overview of createAsyncThunk . &lt;/p&gt;

&lt;p&gt;Thank you for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
    </item>
  </channel>
</rss>
