<?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 Shivam Agrawal on Medium]]></title>
        <description><![CDATA[Stories by Shivam Agrawal on Medium]]></description>
        <link>https://medium.com/@sg9827252555?source=rss-87bba241368f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cVcl17F9HV_14bimOdd4Bg.jpeg</url>
            <title>Stories by Shivam Agrawal on Medium</title>
            <link>https://medium.com/@sg9827252555?source=rss-87bba241368f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 04:51:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sg9827252555/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[Using Nested setTimeout Worthy?]]></title>
            <link>https://medium.com/@sg9827252555/using-nested-settimeout-worthy-beaf61a48cf6?source=rss-87bba241368f------2</link>
            <guid isPermaLink="false">https://medium.com/p/beaf61a48cf6</guid>
            <category><![CDATA[settimeout]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[Shivam Agrawal]]></dc:creator>
            <pubDate>Wed, 31 Aug 2022 17:04:00 GMT</pubDate>
            <atom:updated>2022-08-31T20:16:27.423Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Qg4-MMWntdQhz4Ai.jpg" /></figure><p>There exist a lot of services that are required to be executed either after some defined time or in a defined interval. For running some functionality after some time, we use<strong> setTimeout which takes some piece of code that executes after the defined time</strong>. For running some functionality after some regular intervals, we generally use <strong>setInterval,</strong> <strong>which takes some piece of code that executes after every defined time. </strong>But here exist some issues in using setInterval. Let&#39;s explore them.</p><pre>const func = () =&gt; {<br>  activity A;   //having time consuming synchronous computations<br>  activity B;   //having time consuming synchronous computations<br>}<br>setInterval(func, 1000);</pre><p>Let&#39;s suppose there is a function fun that needs to be called after every 1000 ms, the timer thread in the browser starts counting down, and when time up puts the callback function(in our case func) in the javascript thread’s execution stack. To better understand, we will break down our execution in time.</p><p>At T=0 sec(initially): We have function func in our stack with the attached timer of 1 sec.</p><p>At T=1 sec: The function present in our stack popped out and starts executing. At the same time, function func again pushed into the stack with a timer of 1 sec.</p><p>Now two cases arise,</p><p>Case 1: When the function body executes within 1 second.</p><p>This case is normal, and the code will behave as expected.</p><p>Case 2: When the function body executes in more than 1 second.</p><p>In this case, the engine waits for the function func to complete, then checks the scheduler and runs it immediately if the time is up. In the edge case, if the function func always executes longer than 1000 ms, then the calls will happen without a pause at all.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*dP4O0YzKS1ggaJCMk_ZXSQ.png" /><figcaption>Function calls for setInterval</figcaption></figure><p><strong>Note: The real delay between func calls for setInterval is less than in the code!</strong></p><p>Now, what will be the solution for this? We can use <strong>Nested setTimeout. </strong>Let me tell you how.</p><pre>const func = () =&gt; {<br>  activity A;   //having time consuming synchronous computations <br>  activity B;   //having time consuming synchronous computations<br>  setTimeout(func, 1000);<br>}<br>setTimeout(func, 1000);</pre><p>Again, we are going to break down our execution in time.</p><p>At T=0 sec(initially): We have function func in our stack with the attached timer of 1 sec.</p><p>At T=1 sec: The function present in our stack popped out and started executing, now after completion of Activity A and B (Let&#39;s suppose it takes x second)</p><p>At T= (1+x) sec: Now setTimeout executes, which again pushes the function func in the stack with the attached timer of 1 second.</p><p>Similarly, after ((1+x )+ (1)) sec again function func start executing and this go on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*QCLZL8-gCO7lJ6e5G14zSQ.png" /><figcaption>Function calls for setTimeout</figcaption></figure><p>If you notice something, <strong>The nested setTimeout guarantees the fixed delay (here 1s) in executing the function.</strong></p><p>There exist more benefits of using nested setTimeout instead of setTimeout, as <strong>nested setTimeout is more flexible </strong>as we can schedule the next call according to our requirements. Let’s see how.</p><pre>delay = 1000;<br>const func = () =&gt; {<br>  activity A;   //having time consuming synchronous computations <br>  activity B;   //having time consuming synchronous computations<br>  delay += 1000;<br>  setTimeout(func, delay);<br>}<br>setTimeout(func, 1000);</pre><p>From the above piece of code, the function&#39;s execution happens in an arithmetic progression series where the common difference is 1000 ms, and starting value is 1000 ms.</p><p><strong>Usage:</strong></p><p>Ex: For instance, if we need to write a service that sends a request to the server every 8 seconds asking for data, but if the server is overloaded, it should increase the interval to 8, 16, or 24 seconds…</p><p>Ex: If the functions we’re scheduling are CPU-hungry, then we can measure the time taken by the execution and plan the next call sooner or later.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*WRAZmkP8ekpHAqh5" /></figure><p>If you are thinking of using setInterval in your piece of code, stop for a minute and consider whether we can use nested setTimeout.<strong> It’s still worthy.</strong></p><p>For more info:</p><p>LinkedIn: <a href="https://www.linkedin.com/in/shivam-agrawal-a4a414181/">https://www.linkedin.com/in/shivam-agrawal-a4a414181/</a></p><p>GitHub: <a href="https://github.com/shivam1192">https://github.com/shivam1192</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=beaf61a48cf6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Babel — A magical middle layer]]></title>
            <link>https://medium.com/@sg9827252555/babel-a-magical-middle-layer-710a37f5b54d?source=rss-87bba241368f------2</link>
            <guid isPermaLink="false">https://medium.com/p/710a37f5b54d</guid>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[ast]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Shivam Agrawal]]></dc:creator>
            <pubDate>Sun, 28 Aug 2022 20:01:19 GMT</pubDate>
            <atom:updated>2022-08-28T20:01:19.674Z</atom:updated>
            <content:encoded><![CDATA[<h3>Babel — A magical middle layer</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*xmXfrjuBBheFTkJf.png" /></figure><p>If we talk about browsers, presently there are a lot of options available whether it’s Google Chrome, Mozilla Firefox, Microsoft Edge, Brave, etc (Do let me know your favorite browser, mine is Brave). If you had used the console of the browser, you surely had noticed that you can run Javascript code there.<br>Ever thought about how that Javascript code compiled but not Java or C code? The reason is the browser has an inbuilt Javascript compiler(to be specific its interpreter) that helps in interpreting the Javascript code. Is every browser have a common interpreter? No, Each and every browser have its own interpreter such as Google Chrome has V8, Mozilla Firefox has SpiderMonkey and the list goes on. Are these interpreters on the same page, I mean, does every interpreter execute every newly updated Javascript syntax? No, though most of the interpreters try to be updated. But they are prone to mismatch. Sometimes this can create a bad experience for the user, as some functionality is working in the x browser but not in the y browser. There has to be some way so that this mismatching can be avoided.</p><p>So, here come something called <strong>Babel</strong>. I guess many of the readers had already heard about it. It’s a transpiler(<strong>Transpilers, or source-to-source compilers, are tools that read source code written in one programming language and produce the equivalent code in another language that is in the same level.</strong>), that takes your javascript code and converts this code into the code that is supported by all browsers. If you had worked on a javascript project, you had noticed files like “main.dist.js”, this file is actually the Javascript code that is being supported by all browsers.</p><p><strong>Using Babel comes with the trade-off as it sometimes becomes costly due to an increase in the bundle size of the javascript file, so not every project of Javascript needs Babel.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/0*UUCuPcbyukiMueNz" /></figure><p>Ever thought about how it works internally?</p><p>There involve three major phases:</p><p><strong>Parsing and lexing:</strong> This phase takes your code as a string and by doing some kind of manipulation, it converts it into a tree(specific Abstract Syntax Tree). You can think of an AST as just a piece of JSON having property and value. If you want to play around with AST, visit<strong> </strong><a href="https://astexplorer.net/"><strong>AST Explorer</strong></a>. The most famous parser available is Babylon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_MNYQoGW7GD0VvRagIKMsQ.png" /><figcaption>AST Conversion of simple Javascript code in JSON format</figcaption></figure><p><strong>Transformation:</strong> This phase takes AST and by doing some kind of transformation, it converts it into another AST. The transformations here are doing all the <strong>magic</strong> by converting the specific Javascript syntax to the syntax which is understandable by all browsers. So there are plugins available for the same such as babel/core and babel/transform.</p><p><strong>Code Generation: </strong>This phase takes AST and with the help of generators it generates the Javascript code. The plugin helpful in generating the code is babel/generator.</p><p>Babel is not only used in converting javascript code but it is also used in compiling typescript, converting JSX code into a readable format, etc.</p><p>That’s all for now, Hope you enjoyed the reading.</p><p>If you want to understand more about Babel, I am attaching some of the great resources.</p><p><a href="https://www.youtube.com/watch?v=hOghLCWohuU&amp;t=1485s&amp;ab_channel=ReactVienna">Demystifying Babel</a></p><p><a href="https://www.youtube.com/watch?v=F1BIeVe99EQ&amp;ab_channel=MelbJS">Babel: Beyond the Basics — Sebastian McKenzie, Creator of Babel</a></p><p><a href="https://www.youtube.com/watch?v=xnx9Ft3QleI&amp;ab_channel=RedBadger">React London Meetup May 2015</a></p><p>For more info:</p><p>LinkedIn: <a href="https://www.linkedin.com/in/shivam-agrawal-a4a414181/">https://www.linkedin.com/in/shivam-agrawal-a4a414181/</a></p><p>GitHub: <a href="https://github.com/shivam1192">https://github.com/shivam1192</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=710a37f5b54d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Smart Contract | A world without mediators]]></title>
            <link>https://medium.com/@sg9827252555/smart-contract-a-world-without-mediators-c4f9ee3feb92?source=rss-87bba241368f------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4f9ee3feb92</guid>
            <category><![CDATA[future]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[blockchain-technology]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Shivam Agrawal]]></dc:creator>
            <pubDate>Mon, 18 May 2020 18:02:41 GMT</pubDate>
            <atom:updated>2020-05-18T18:02:41.383Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9-1adeliCA6wt18D.png" /></figure><p>Smart contracts are lines of code that are stored on a blockchain and automatically execute when predetermined terms and conditions are met. At the most basic level, they are programs that run as they’ve been set up to run by the people who developed them.</p><p>In a smart contract approach, an asset or currency is transferred into a program and the program runs this code and at some point, it automatically validates a condition and it automatically determines whether the asset should go to one person or back to the other person, or whether it should be immediately refunded to the person who sent it or some combination thereof.</p><p>According to me, very soon Blockchain Technology will replace lawyers or middleman!</p><p><strong>But wait a minute! Why should we trust smart contracts?</strong></p><p>Well because the smart contract is stored on a Blockchain, they inherit some interesting properties. They are</p><ul><li>Immutable</li><li>Distributed</li></ul><p>Being immutable means once a smart contract is created, it can never be changed again. So no one can go behind your back and tamper with the code of your contract. And being distributed means that the output of your contract is validated by everyone on the network.<br><em>Muh-haha! Interesting…right?</em></p><p>Right now there are a handful of blockchains that support smart contracts, but the biggest one is Ethereum. It was specially created and designed to support smart contracts. They can be programmed in a special programming language called <strong>Solidity</strong>. Solidity is object-oriented, high-level language for implementing the smart contracts.</p><p>Now, we will make our first smart contract — PrintName in solidity language which will update or show the state variable initialized with the “Hello World” string. We will use an online IDE — <a href="https://remix.ethereum.org/">Remix</a> for writing, compiling, and deploying the smart contracts. you will see the layout of Remix IDE, as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RMG2Bcuf3fU6P6-D0u2Nlw.png" /></figure><p>The left side shows a list of Solidity files. Now, let’s create a new Solidity file, called “PrintName.sol”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ECKOE-eG2yI-V7Cq4F7vxw.png" /></figure><p>The middle part is the code writing and editing section, which also provides syntax highlighting mapped to Solidity keywords. Compilation warnings and errors will be displayed in the gutter.</p><p>Let’s start writing our smart contract:</p><pre>pragma solidity &gt;=0.4.22 &lt;0.7.0;<br>contract PrintName{<br>   //Your code goes here!!<br>}</pre><p>The above code defines the version of the solidity and initializes our contract PrintName. All the logic, functions, and variables are declared or written inside the PrintName block.</p><pre>pragma solidity &gt;=0.4.22 &lt;0.7.0;<br>contract PrintName{<br> string value;</pre><pre>constructor() public{<br>    value = &quot;Hello World&quot;;<br> }<br>}</pre><p>The above code declares the value as a string data type. It is generally a state variable. State variables are variables whose values are permanently stored in contract storage. Every state or local variable has a specified data type. Like any other high-level language Solidity also have different data type like uint256, bool, string, address(use to store the public address of the account), mapping(use to map one data type with another data type), struct, etc.</p><p>Then we call the constructor function which is used to initialize the state variable of our smart contract. A contract can have only one constructor. In our case, we initialize our state variable value as “Hello World”.</p><p>We also have to mention the visibility of the function in solidity. This visibility is placed immediately following the function parameter list. There are four visibility keywords:</p><ol><li>Private: A private function is one that can only be called by the main contract itself. Although it’s not the default, it is generally a good practice to keep your functions private unless a scope with more visibility is needed.</li><li>Internal: An internal function can be called by the main contract itself, plus any derived contracts. As with private functions, it’s generally a good idea to keep your functions internal wherever possible.</li><li>External: An external function can only be called from a third party. It cannot be called from the main contract itself or any contracts derived from it.</li><li>Public: Finally, a public function can be called from all potential parties. Unless otherwise specified, all functions are made public by default.</li></ol><p>In our case, we set constructor visibility to be public.</p><p>Let’s go through the function syntax in solidity:</p><pre>function function_name(parameters) &lt;visibility&gt; &lt;modifiers&gt; returns(){<br>    //statements and logic<br>}</pre><p>Like any other function in high- level language, it has parameters to be given and returns data type. We have to mention the visibility scope as discussed earlier. We also have to mention the modifiers. It creates additional features on function or apply some restriction on function. The most commonly used function modifiers are the <strong>view</strong> and <strong>pure</strong>. The view simply means <strong>“this function is read-only.”</strong> The function with the ‘view’ modifier only executes without any data being written or overwritten. The pure means a pure function, which is a function to return a value using only the parameter of the function without any side effects(bring outer variable and changes it).</p><pre>pragma solidity &gt;=0.4.22 &lt;0.7.0;</pre><pre>contract PrintName{<br> string value;</pre><pre>constructor() public{<br>    value = “Hello World”;<br> }</pre><pre>function getName() public view returns(string memory){<br>    return value;<br> }</pre><pre>function setName(string memory _value) public{<br>    value = _value;<br> }<br>}</pre><p>The above code declares two functions getName and setName. Let’s go one by one, getName function returns the state variable value with visibility mode as public and modifier as view and setName takes a parameter as string _value and visibility as public, inside the function we will assign state variable value to the parameter passed in the function.</p><p>So great huh! we wrote our first smart contract and now it is ready to be compiled and deployed.</p><p>Before it, we have to activate modules in the remix. Go to the plugin manager and activate SOLIDITY COMPILER and DEPLOY &amp; RUN TRANSACTIONS. After activating we are ready for compilation and deployment.</p><p>Go to the solidity compiler option and click <strong>Compile PrintName.sol</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uRrYJo9s1415V1D7epqhWg.png" /><figcaption>Green mark on the icon shows that the contract is successfully compiled</figcaption></figure><p>After successful compilation go to Deploy and Run Transaction, click on deploy with below settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XNrV46USwGFhnin0cQmAyA.png" /><figcaption>Before deploying</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*knXhUaRdpcVS0SbDVUEBVw.png" /><figcaption>After deploying</figcaption></figure><p>Now you can call function accordingly.</p><p>Hey hey! You have created your first smart contract :)</p><p>For more info refer to this:</p><p>LinkedIn: <a href="https://www.linkedin.com/in/shivam-agrawal-a4a414181/">https://www.linkedin.com/in/shivam-agrawal-a4a414181/</a></p><p>GitHub: <a href="https://github.com/shivam1192">https://github.com/shivam1192</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4f9ee3feb92" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>