<?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: Aishanii</title>
    <description>The latest articles on Forem by Aishanii (@aishanipach).</description>
    <link>https://forem.com/aishanipach</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%2F596136%2F3015fc80-6490-41f9-aadb-50214a23f007.jpeg</url>
      <title>Forem: Aishanii</title>
      <link>https://forem.com/aishanipach</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aishanipach"/>
    <language>en</language>
    <item>
      <title>Day 0: System Architecture basics</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Tue, 25 Mar 2025 16:22:45 +0000</pubDate>
      <link>https://forem.com/aishanipach/day-0-system-architecture-basics-1md9</link>
      <guid>https://forem.com/aishanipach/day-0-system-architecture-basics-1md9</guid>
      <description>&lt;h2&gt;
  
  
  Introduction📚
&lt;/h2&gt;

&lt;p&gt;This first post will just be an introduction to basic terminologies and key concepts one should know before we start working with case studies. I aim to write about the foundational concepts which are useful for system design interview.&lt;/p&gt;

&lt;h3&gt;
  
  
  High-Level Architecture of a Computer System 📕
&lt;/h3&gt;

&lt;p&gt;System design begins with understanding the high-level architecture of a computer. The fundamental building blocks include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Bits and Bytes&lt;/strong&gt;: The smallest unit of data in computing is a bit (0 or 1). Aggregations of bits lead to bytes (8 bits) and further into kilobytes, megabytes, gigabytes, and terabytes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Storage&lt;/strong&gt;: Storage devices can either be Hard Disk Drives (HDD) or Solid State Drives (SSD). SSDs are more expensive but significantly faster in data retrieval, boasting speeds up to five times that of HDDs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RAM&lt;/strong&gt;: Random Access Memory (RAM) is volatile memory that serves as the primary workspace for currently running applications and data. It provides rapid read and write access, which is crucial for efficient processing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cache Memory&lt;/strong&gt;: This is a smaller, faster memory located closer to the CPU, designed to decrease data access times by storing frequently used data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CPU&lt;/strong&gt;: The central processing unit (CPU) is the brain of the computer that executes instructions by compiling high-level programming languages into machine code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Motherboard&lt;/strong&gt;: Acts as the communication backbone that interconnects all components.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Production-Ready App Architecture 📗
&lt;/h3&gt;

&lt;p&gt;The architecture of a production-ready application integrates several key components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CI/CD Pipeline&lt;/strong&gt;: Continuous Integration and Continuous Deployment automate the code deployment process, ensuring seamless transitions from development to production without manual intervention.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Load Balancers&lt;/strong&gt;: These distribute incoming user requests across multiple servers, ensuring reliable performance during peak traffic.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;External Storage Solutions&lt;/strong&gt;: Storage systems are kept external to the main production server, interlinked through a network.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Logging and Monitoring Systems&lt;/strong&gt;: These systems record and analyse application performance, allowing real-time error detection and user notification through platforms like Slack.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Debugging Process 📘
&lt;/h3&gt;

&lt;p&gt;When an error occurs, the debugging process comprises:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Error Identification&lt;/strong&gt;: Use logs to find patterns or anomalies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replication&lt;/strong&gt;: Recreate the error in a non-production environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analysis&lt;/strong&gt;: Employ tools to inspect the running application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hotfix Deployment&lt;/strong&gt;: Implement temporary fixes swiftly to restore functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key Principles of System Design🥸
&lt;/h3&gt;

&lt;p&gt;Good system design embodies several essential principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Systems must accommodate increases in users and data traffic gracefully.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt;: Future developers should be able to understand and improve the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Resources need to be optimally utilized.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally, systems must be designed with the understanding that failures are inevitable, ensuring they remain robust under adverse conditions.&lt;/p&gt;

&lt;h3&gt;
  
  
  CAP Theorem 🧢
&lt;/h3&gt;

&lt;p&gt;The CAP theorem, formulated by Eric Brewer, delineates key trade-offs for distributed systems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Consistency&lt;/strong&gt;: All nodes must reflect the same data state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Availability&lt;/strong&gt;: The system remains operational, serving requests consistently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition Tolerance&lt;/strong&gt;: Operations persist even in the face of network failures.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A distributed &lt;strong&gt;system can only achieve two out of these three characteristics at a time&lt;/strong&gt;, necessitating a thoughtful approach to designing systems based on specific business needs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Measuring Availability and Reliability&lt;/strong&gt;&lt;br&gt;
Availability refers to the operational uptime of the system, typically expressed as a percentage.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Networking Basics 🛜
&lt;/h3&gt;

&lt;p&gt;Networking fundamentals focus on how devices communicate through:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IP Addressing&lt;/strong&gt;: Distinguishing devices on a network (IPv4 and IPv6).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Packets&lt;/strong&gt;: Units of data formatted for transmission, containing IP headers to facilitate communication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transport Layer Protocols&lt;/strong&gt;: TCP guarantees reliable packet delivery, while UDP provides faster, less reliable transmission, suitable for time-sensitive communications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Application Layer Protocols 📙
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Common application layer protocols include:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;HTTP&lt;/strong&gt;: A stateless protocol for web transactions, suitable for standard web operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebSockets&lt;/strong&gt;: Allow for real-time, two-way communication over a single connection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SMTP&lt;/strong&gt;: Standard used for sending emails between servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FTP&lt;/strong&gt;: Used for file transfers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GRPC and GraphQL&lt;/strong&gt;: Modern methodologies for creating APIs, offering advanced features and flexible queries&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  API Design Best Practices🙌
&lt;/h3&gt;

&lt;p&gt;Creating exceptional APIs involves defining clear input and output operations. Best practices include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RESTful Principles&lt;/strong&gt;: Apply standardized HTTP methods and ensure statelessness.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GraphQL&lt;/strong&gt;: Allows clients to request specific datasets, reducing data over-fetching.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rate Limiting&lt;/strong&gt;: Prevent abuses of the API through controlled requests.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Strategies for Reducing Latency ♻️
&lt;/h3&gt;

&lt;p&gt;Latency reduction techniques such as caching and content delivery networks (CDNs) are pivotal in modern web development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: Storing copies of data to expedite access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CDN&lt;/strong&gt;: Geographically distributed servers that deliver content closer to users.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Proxy Servers📒
&lt;/h3&gt;

&lt;p&gt;Proxy servers act as intermediaries between clients and servers, fulfilling various roles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Forward Proxy&lt;/strong&gt;: Acts on behalf of clients, anonymizing requests and managing internet access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reverse Proxy&lt;/strong&gt;: Disguises backend servers, balancing load and managing SSL encryption.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Load Balancing🎢
&lt;/h3&gt;

&lt;p&gt;Load balancing distributes workloads across multiple servers to prevent bottlenecks. Strategies include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Round Robin&lt;/strong&gt;: Sequential allocation of requests to servers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Least Connection&lt;/strong&gt;: Directs traffic to servers with minimal active connections.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Geographical Algorithms&lt;/strong&gt;: Routes requests based on user proximity to servers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Database Fundamentals📈
&lt;/h3&gt;

&lt;p&gt;Understanding databases is crucial for system design. Key points include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Relational vs. NoSQL Databases&lt;/strong&gt;: SQL databases offer structured data management, while NoSQL databases provide flexibility for unstructured data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sharding and Replication&lt;/strong&gt;: Techniques for horizontal scaling to enhance availability and performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching and Indexing&lt;/strong&gt;: Important performance optimization techniques.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>programming</category>
      <category>systems</category>
      <category>career</category>
      <category>git</category>
    </item>
    <item>
      <title>include NSPrivacyAccessedAPICategoryDiskSpace &amp; NSPrivacyAccessedAPICategoryFileTimestamp Information🤔</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Mon, 01 Apr 2024 19:02:15 +0000</pubDate>
      <link>https://forem.com/aishanipach/include-nsprivacyaccessedapicategorydiskspace-nsprivacyaccessedapicategoryfiletimestamp-information-59hi</link>
      <guid>https://forem.com/aishanipach/include-nsprivacyaccessedapicategorydiskspace-nsprivacyaccessedapicategoryfiletimestamp-information-59hi</guid>
      <description>&lt;p&gt;Hey, so apple just came out of nowhere and sent a big email outlining something like NSPrivacyAccessedAPICategoryFileTimestamp Information, NSPrivacyAccessedAPICategoryDiskSpace and NSFileSystemFreeSize with few other API declaration.&lt;/p&gt;

&lt;p&gt;Here's what it means for you as a developer and here is what you can do.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to release my current app😵‍💫
&lt;/h2&gt;

&lt;p&gt;Don't worry till May 1st, Apple doesn't expect you to declare these. In fact, they would just flag the required API declaration and approve your app for now. So pushing to production won't stop🥳&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I get what are the declarations required before submission on Appstore?
&lt;/h2&gt;

&lt;p&gt;You can run a bash script to check your codebase and dependencies for such API declaration requirements.&lt;/p&gt;

&lt;p&gt;Use the following one:&lt;br&gt;
&lt;a href="https://github.com/Wooder/ios_17_required_reason_api_scanner/blob/main/required_reason_api_text_scanner.sh" rel="noopener noreferrer"&gt;https://github.com/Wooder/ios_17_required_reason_api_scanner/blob/main/required_reason_api_text_scanner.sh&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Download the &lt;code&gt;required_reason_api_scanner_text.sh&lt;/code&gt; file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to terminal and go to the directory where this file was downloaded to. Run the following command with your app directory path.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sh required_reason_api_scanner.sh &amp;lt;AppDirectory&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will give you a list of declarations, in the following format:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Found potentially required reason API usage 'UserDefaults' &lt;br&gt;
in './ViewController.swift' Line numbers: 128&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search for the API declarations mentioned here:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api" rel="noopener noreferrer"&gt;https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_use_of_required_reason_api&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Go to Xcode (15 and above)&lt;/li&gt;
&lt;li&gt;File -&amp;gt; New -&amp;gt; File&lt;/li&gt;
&lt;li&gt;Search and select the 'App privacy' option&lt;/li&gt;
&lt;li&gt;The default privacy file name is required, do not change that and continue&lt;/li&gt;
&lt;li&gt;Copy the following file, and edit the newly created &lt;code&gt;PrivacyInfo.xcprivacy&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&amp;gt;
&amp;lt;plist version="1.0"&amp;gt;
&amp;lt;dict&amp;gt;
 &amp;lt;key&amp;gt;NSPrivacyAccessedAPITypes&amp;lt;/key&amp;gt;
 &amp;lt;array&amp;gt;
  &amp;lt;dict&amp;gt;
   &amp;lt;key&amp;gt;NSPrivacyAccessedAPIType&amp;lt;/key&amp;gt;
   &amp;lt;string&amp;gt;NSPrivacyAccessedAPICategoryUserDefaults&amp;lt;/string&amp;gt;
   &amp;lt;key&amp;gt;NSPrivacyAccessedAPITypeReasons&amp;lt;/key&amp;gt;
   &amp;lt;array&amp;gt;
    &amp;lt;string&amp;gt;CA92.1&amp;lt;/string&amp;gt;
   &amp;lt;/array&amp;gt;
  &amp;lt;/dict&amp;gt;
 &amp;lt;/array&amp;gt;
&amp;lt;/dict&amp;gt;
&amp;lt;/plist&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You're done!&lt;br&gt;
Build your app, sit back and relax.⛱️&lt;/p&gt;

</description>
      <category>ios</category>
      <category>apple</category>
      <category>reactnative</category>
    </item>
    <item>
      <title>Bun is breaking other package managers</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Wed, 20 Sep 2023 22:39:14 +0000</pubDate>
      <link>https://forem.com/aishanipach/bun-is-breaking-other-package-managers-4bhe</link>
      <guid>https://forem.com/aishanipach/bun-is-breaking-other-package-managers-4bhe</guid>
      <description>&lt;p&gt;A new javascript runtime is the central thing on the internet and I think it's time for new sticker on my laptop I code JS on.&lt;/p&gt;

&lt;p&gt;But how is Bun so performant?⚡️&lt;/p&gt;

&lt;h2&gt;
  
  
  Built from scratch🧑‍🍳
&lt;/h2&gt;

&lt;p&gt;Bun is not built on top of nodejs, rather they have used Zig over Webkit (for Safari) and internally optimised for performance by getting into the basics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caching and linking internally📁
&lt;/h2&gt;

&lt;p&gt;Once installed, packages are needed to be called in node_modules and for that they have dedicatedly taken OS in consideration. While in linux syscalls help make internal calls faster, in macOS that is not available, hence they copy all the files that costs memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  The APIs✍️
&lt;/h2&gt;

&lt;p&gt;The APIs are in Native code and not in javasript which not only exposes them to machine level code but also gives them freedom to look into things at a deeper level.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are many more good things which can be found here:&lt;br&gt;
&lt;a href="https://bun.sh/blog/bun-v1.0" rel="noopener noreferrer"&gt;https://bun.sh/blog/bun-v1.0&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The guy to follow to get updates is here: &lt;a href="https://twitter.com/jarredsumner" rel="noopener noreferrer"&gt;https://twitter.com/jarredsumner&lt;/a&gt; (literally the creator tweeting changes incoming)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>npm</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>DeprecationWarning: crypto.createCipher is deprecated.</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Mon, 29 May 2023 17:55:24 +0000</pubDate>
      <link>https://forem.com/aishanipach/deprecationwarning-cryptocreatecipher-is-deprecated-4dgg</link>
      <guid>https://forem.com/aishanipach/deprecationwarning-cryptocreatecipher-is-deprecated-4dgg</guid>
      <description>&lt;p&gt;Support for both &lt;code&gt;crypto.createCipher&lt;/code&gt; &amp;amp; &lt;code&gt;crypto.createDecipher&lt;/code&gt; has been pulled as they use a weak key derivation function (MD5 with no salt) and static initialization vectors (iv).&lt;/p&gt;

&lt;p&gt;Here, we'll work with migrating from &lt;code&gt;crypto.createCipher&lt;/code&gt; &amp;amp; &lt;code&gt;crypto.createDecipher' to&lt;/code&gt;crypto.createCipheriv&lt;code&gt;&amp;amp;&lt;/code&gt;crypto.createDecipheriv`&lt;/p&gt;

&lt;p&gt;Let's begin with the basics. Run the following command in parent directory of your node.js project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install crypto --save&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Encryption
&lt;/h2&gt;

&lt;p&gt;Make sure that the secret key &amp;amp; secret initialization vector is set correctly with required length. To keep the format intact we use &lt;code&gt;createHash&lt;/code&gt; from crypto.&lt;/p&gt;

&lt;p&gt;Here is the code below:&lt;/p&gt;

&lt;p&gt;var Crypto = require('crypto')&lt;br&gt;
var secret_key = 'fd85b494-aaaa'&lt;br&gt;
var secret_iv = 'smslt'&lt;br&gt;
var encryptMethod = 'AES-256-CBC';&lt;br&gt;
var key = Crypto.createHash('sha512').update(secret_key, 'utf-8').digest('hex').substr(0, 32);&lt;br&gt;
var iv = Crypto.createHash('sha512').update(secret_iv, 'utf-8').digest('hex').substr(0, 16);&lt;br&gt;
var encryptMsg = encryptStr("Encrypting", encryptMethod, key, iv);&lt;br&gt;
console.log(encryptedMsg);&lt;/p&gt;

&lt;p&gt;function encryptStr(plain_text, encryptMethod, secret, iv) {&lt;br&gt;
   var encryptor = Crypto.createCipheriv(encryptMethod, secret, iv);&lt;br&gt;
   var aes_encrypted = encryptor.update(plain_text, 'utf8', &lt;br&gt;
 'base64') + encryptor.final('base64');&lt;/p&gt;

&lt;p&gt;return Buffer.from(aes_encrypted).toString('base64');&lt;br&gt;
};&lt;/p&gt;

&lt;h2&gt;
  
  
  Decryption
&lt;/h2&gt;

&lt;p&gt;Here is the code below:&lt;/p&gt;

&lt;p&gt;var Crypto = require('crypto');&lt;br&gt;
var secret_key = 'fd85b494-aaaa';&lt;br&gt;
var secret_iv = 'smslt';&lt;br&gt;
var encryptMethod = 'AES-256-CBC';&lt;br&gt;
var decryptMessage = decryptStr(encryptText, encryptionMethod, key , iv);&lt;br&gt;
console.log(decrptMessage);&lt;/p&gt;

&lt;p&gt;function decryptStr(encryptText, encryptMethod, secret, iv) {&lt;br&gt;
  const buff = Buffer.from(encryptText, 'base64');&lt;br&gt;
  encryptedMessage = buff.toString('utf-8');&lt;br&gt;
  var decryptor = Crypto.createDecipheriv(encryptMethod, secret, iv);&lt;br&gt;
  return decryptor.update(encryptText, 'base64', 'utf8') + decryptor.final('utf8');&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://nodejs.org/api/crypto.html" rel="noopener noreferrer"&gt;Crypto|Node.js v20.2.0 Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Don't use '&amp;&amp;' for conditional reasoning</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Wed, 14 Dec 2022 18:56:06 +0000</pubDate>
      <link>https://forem.com/aishanipach/dont-use-for-conditional-reasoning-3525</link>
      <guid>https://forem.com/aishanipach/dont-use-for-conditional-reasoning-3525</guid>
      <description>&lt;p&gt;We often use '&amp;amp;&amp;amp;' to display a component if a given condition is true and to not display it when the condition is false.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BigComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;After&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="nx"&gt;we&lt;/span&gt; &lt;span class="nx"&gt;will&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;based&lt;/span&gt; 
             &lt;span class="nx"&gt;on&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;condition&lt;/span&gt; 
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;NextComponent&lt;/span&gt;&lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;BigComponent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So basically, here the condition is evaluated and only if the left side of '&amp;amp;&amp;amp;' is true, we move forward and then render the second half, that is the component.&lt;/p&gt;

&lt;p&gt;This is called short circuiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  There are two problems here:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The output to the condition must be &lt;strong&gt;boolean&lt;/strong&gt;. If the condition gives 0, it will render 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the result from the condition is undefined, it will give an &lt;strong&gt;error&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What to do then?
&lt;/h2&gt;

&lt;p&gt;To avoid something like displaying a zero or getting an 🔴error🔴, we can use a &lt;strong&gt;ternary operator&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;condition ? &amp;lt;ConditionalComponent /&amp;gt; : null&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which translates to: "if &lt;em&gt;condition&lt;/em&gt; is true do the first statement else execute the statement after the colon (:)"&lt;/p&gt;

&lt;h3&gt;
  
  
  Happy Coding!👩‍💻
&lt;/h3&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>What's Bit Manipulation?</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Tue, 23 Aug 2022 10:24:17 +0000</pubDate>
      <link>https://forem.com/aishanipach/whats-bit-manipulation-5chj</link>
      <guid>https://forem.com/aishanipach/whats-bit-manipulation-5chj</guid>
      <description>&lt;p&gt;Bit Manipulation- &lt;a href="https://450dsa.com/bit_manipulation" rel="noopener noreferrer"&gt;https://450dsa.com/bit_manipulation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For X and Y such that &lt;br&gt;
X/2=Y&lt;/p&gt;

&lt;p&gt;Example:  &lt;/p&gt;

&lt;h3&gt;
  
  
  - 7/2=3
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;7-&amp;gt;111 (3)
3-&amp;gt;011 (2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  - 12/2=6
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12-&amp;gt;1100 (2)
6-&amp;gt;0110 (2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the X is &lt;strong&gt;ODD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It will have the least significant bit set. Hence dividing by 2, will be right shifting&lt;/p&gt;

&lt;p&gt;If the X is &lt;strong&gt;EVEN&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Its least significant bit will not be set hence dividing by 2 will have no effect on number of set bits as only 0 will be lost.&lt;/p&gt;

&lt;h3&gt;
  
  
  So if the number if the X is ODD,
&lt;/h3&gt;

&lt;p&gt;number of set bits of X = 1 + Y&lt;/p&gt;

&lt;h3&gt;
  
  
  But when X is even,
&lt;/h3&gt;

&lt;p&gt;number of set bits of X = Y&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>algorithms</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What does javascript engine do?👩‍💻</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Tue, 02 Aug 2022 12:36:29 +0000</pubDate>
      <link>https://forem.com/aishanipach/what-does-javascript-engine-do-3hid</link>
      <guid>https://forem.com/aishanipach/what-does-javascript-engine-do-3hid</guid>
      <description>&lt;p&gt;For starters &lt;strong&gt;javascript runtime environment is not the javascript engine&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Javascript engine is inside the javascript runtime environment which is different for different implementations. A javascript engine which is most famous is V8 (Chrome and node.js), Mozilla uses Spidermonkey which was the first js engine.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parsing👇
&lt;/h3&gt;

&lt;p&gt;Here the code is broken down to smaller pieces known as tokens and is then transformed. &lt;/p&gt;

&lt;p&gt;The code that is entered is turned into an &lt;strong&gt;abstract syntax tree&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Optimization &amp;amp; compilation👇
&lt;/h3&gt;

&lt;p&gt;Javascript can be treated as &lt;strong&gt;both interpreted or compiled language&lt;/strong&gt;. Spidermonkey treated it as an interpreted language for its fast use in browsers.&lt;/p&gt;

&lt;p&gt;Nowadays, both are used, in &lt;strong&gt;just in time compiled&lt;/strong&gt; javascript form. &lt;/p&gt;

&lt;p&gt;The abstract syntax tree is broken down into bytes here and sent forward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Execution👇
&lt;/h3&gt;

&lt;p&gt;This is done along with the two of our favourite components of JS, &lt;strong&gt;Call Stack &amp;amp; Memory Heap&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F06batzdq5fxrmkgdic44.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F06batzdq5fxrmkgdic44.png" alt="Image description" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The logic of different JS engine is different from one another. Some of the features that are combined to make the best out it are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inline caching: It takes note of the repeated calls to the same method that tend to occur on the same type of object.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mark and Sweep: This is a garbage collection algorithm where the garbage collector does marking in first phase &amp;amp; sweeping in second.&lt;br&gt;
All the reachable in use objects are ticked as true and marked while others are marked as 0. Then we sweeps the unreachable objects from heap memory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;For more on javascript runtime environment &lt;br&gt;
🌟&lt;a href="https://medium.com/@gemma.croad/understanding-the-javascript-runtime-environment-4dd8f52f6fca#:~:text=The%20JavaScript%20runtime%20environment%20provides,and%20make%20the%20code%20work." rel="noopener noreferrer"&gt;Understanding JRE&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Debouncing in javascript✨</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Sat, 30 Jul 2022 11:25:00 +0000</pubDate>
      <link>https://forem.com/aishanipach/debouncing-in-javascript-2k9b</link>
      <guid>https://forem.com/aishanipach/debouncing-in-javascript-2k9b</guid>
      <description>&lt;p&gt;To optimise browser performance as well as keep up with user experience deliverables, there are few techniques, one of which is &lt;strong&gt;debouncing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A very fine example for this would be any search bar. When you type a word in search bar with every letter it shows new recommendations.&lt;/p&gt;

&lt;p&gt;From the surface the trick here must be making an &lt;strong&gt;API call every time somebody enters a letter&lt;/strong&gt; to fetch desirable options according to latest input.&lt;/p&gt;

&lt;p&gt;A better way to do this is through debouncing,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We set a &lt;code&gt;threshold&lt;/code&gt; for a time period, can be 5s or .05 sec.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Every time this threshold timer expires, we make an API call to get data best matching the input.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Clear the timer and reset&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code:
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
&lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;search-bar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;hecticSearchHandler&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;hecticSearchHandler&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

&lt;span class="cm"&gt;/* capture the search query entered by customer */&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* Make an API call with search query */&lt;/span&gt;
&lt;span class="nf"&gt;getSearchResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F5b1ufsm6cnz51fgd08uw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F5b1ufsm6cnz51fgd08uw.png" alt="Image description" width="617" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reusable function code:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;optiSearchHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;debounceFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;debounceFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
       &lt;span class="nf"&gt;clearTimeOut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="nx"&gt;timer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setTimeOut&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
           &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
       &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Watch this video for clarity: &lt;a href="https://www.youtube.com/watch?v=Zo-6_qx8uxg&amp;amp;ab_channel=AkshaySaini" rel="noopener noreferrer"&gt;Debounce interview ques&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Currying in javascript(🔖)</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Thu, 28 Jul 2022 11:58:46 +0000</pubDate>
      <link>https://forem.com/aishanipach/currying-in-javascript-4d4i</link>
      <guid>https://forem.com/aishanipach/currying-in-javascript-4d4i</guid>
      <description>&lt;p&gt;As we were learning before, &lt;strong&gt;functions in javascript&lt;/strong&gt; are&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Treated as sub-programs (execution context)&lt;/li&gt;
&lt;li&gt;First class objects (they have methods &amp;amp; properties)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currying is basically:&lt;br&gt;
&lt;strong&gt;function (a,b,c) --&amp;gt; function (a)(b)(c)&lt;/strong&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Currying using closures👇
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//closure, f is batter here&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;batter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;cook&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;mix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;batter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;flour &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;banana puree&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;a href="https://media2.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%2F4sa6fpe8wu1n4e3t1z8r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F4sa6fpe8wu1n4e3t1z8r.png" alt="Image description" width="432" height="86"&gt;&lt;/a&gt;&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Currying using function methods- bind()👇
&lt;/h2&gt;

&lt;p&gt;As we have discussed before, &lt;code&gt;bind()&lt;/code&gt; is function method used to get a function copy to be used with fixed arguments or &lt;code&gt;this&lt;/code&gt; reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;getTwice&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;multiply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;getTwice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can change the number but getTwice will have &lt;code&gt;a=2&lt;/code&gt; &lt;strong&gt;fixed&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media2.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%2Fq2n0d0az57bkqftea025.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fq2n0d0az57bkqftea025.png" alt="Image description" width="461" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Currying requires the function to have a fixed number of arguments.&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;⭐Read about &lt;a href="https://www.telerik.com/blogs/functional-programming-javascript" rel="noopener noreferrer"&gt;functional programming concepts here.&lt;/a&gt; &lt;br&gt;
I just had to link this one:&lt;br&gt;
⭐&lt;a href="https://codeburst.io/callbacks-closures-and-currying-3cc14300686a" rel="noopener noreferrer"&gt;Closures, currying &amp;amp; functions in js&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>call(), bind() &amp; apply() in javascript</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Wed, 27 Jul 2022 12:41:24 +0000</pubDate>
      <link>https://forem.com/aishanipach/call-bind-apply-in-javascript-5401</link>
      <guid>https://forem.com/aishanipach/call-bind-apply-in-javascript-5401</guid>
      <description>&lt;p&gt;If asked about any one of these keywords in a js interview, one thing they all have in common is they are &lt;strong&gt;function prototype related methods in javascript&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Every function has it's own execution context, as discussed earlier. Every function also has it's own &lt;code&gt;this&lt;/code&gt; keyword, as introduced in ES6. We will be working around with &lt;code&gt;this&lt;/code&gt; keyword using the methods mentioned.&lt;/p&gt;

&lt;h2&gt;
  
  
  call()
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;call&lt;/code&gt; function let's us provide arguments to a function separately. Here, we are passing the value to &lt;code&gt;this&lt;/code&gt; for the function.&lt;/p&gt;

&lt;p&gt;I believe that it's always clearer with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;chime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;notif&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;has followed you!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;notif&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Joe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@Joeisheretocomment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;chime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Foovabeuhp0fw8u1zqr2t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foovabeuhp0fw8u1zqr2t.png" alt="Image description" width="505" height="86"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(one of those days where you do not have your pc and it becomes a default light mode day)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is one of the use cases of &lt;code&gt;call()&lt;/code&gt;. We use call function for constructors and objects too (find references below).&lt;/p&gt;
&lt;h2&gt;
  
  
  apply()
&lt;/h2&gt;

&lt;p&gt;It's similar to call(), just the difference is that we pass the &lt;strong&gt;arguments as an array&lt;/strong&gt; when using apply() method.&lt;/p&gt;

&lt;p&gt;A very interesting use case of &lt;code&gt;apply()&lt;/code&gt; that I found was while using inbuilt libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;988&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fql08i6kyp36f0ta608wy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fql08i6kyp36f0ta608wy.png" alt="Image description" width="368" height="96"&gt;&lt;/a&gt;&lt;br&gt;
Interestingly, we can use apply here to make the &lt;code&gt;Math.max&lt;/code&gt; function take each element of the array be considered as single and separate argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;988&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Fj6jsqhiu3xbk7rv3ym0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj6jsqhiu3xbk7rv3ym0o.png" alt="Image description" width="418" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first parameter is &lt;strong&gt;not used by Math.max&lt;/strong&gt; as it doesn't need any context.&lt;/p&gt;

&lt;h2&gt;
  
  
  bind()
&lt;/h2&gt;

&lt;p&gt;This prototyping method makes another copy of the function and is helpful to hold on to &lt;code&gt;this&lt;/code&gt; reference. &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 javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;follower&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JoeAgain&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;follower&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;funcUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;funcUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, we are binding a function copy with specific &lt;code&gt;**this=user**&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These can be used for &lt;em&gt;function currying, function binding and constructors&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Go through examples and use cases for each of them, they are so widely used, just don't get confused :D&lt;br&gt;
🌟 &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call" rel="noopener noreferrer"&gt;Call &amp;amp; apply method&lt;/a&gt;&lt;br&gt;
🌟&lt;a href="https://medium.com/@omergoldberg/javascript-call-apply-and-bind-e5c27301f7bb" rel="noopener noreferrer"&gt;this keyword and function prototyping&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>setTimeout() &amp; setInterval() in javascript</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Mon, 18 Jul 2022 08:01:25 +0000</pubDate>
      <link>https://forem.com/aishanipach/settimeout-setinterval-in-javascript-83b</link>
      <guid>https://forem.com/aishanipach/settimeout-setinterval-in-javascript-83b</guid>
      <description>&lt;p&gt;As we discussed earlier setTimeout() starts it's timer and goes to task queue and waits until the call stack is empty. This is all a part of the event loop working.&lt;/p&gt;

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

&lt;p&gt;Example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;sayHi&lt;/code&gt; function is executed after a delay of 10000 ms.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;setTimeout()&lt;/code&gt; doesn't necessarily execute the callback function precisely after the time inserted. &lt;/p&gt;

&lt;p&gt;As it waits for the call stack to be empty, it might be the case that the timer has already expired while waiting, which means it will execute after a while and not the time mentioned. &lt;/p&gt;

&lt;p&gt;Hence,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sayHi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even here, it will wait till the whole program is executed and call stack is &lt;strong&gt;empty&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By remaining in task queue, it makes sure to avoid thread blocking. So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It waits &lt;strong&gt;atleast&lt;/strong&gt; the time mentioned to execute the callback.&lt;/li&gt;
&lt;li&gt;It does not block the main thread.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;It keeps on executing the callback function after given interval until stopped.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;timerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;attention&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;timerId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;stop&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F454bljqpr3xrvjn4qdbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F454bljqpr3xrvjn4qdbt.png" alt="Image description" width="767" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the 'attention' is printed after every 2 seconds and it's cleared using &lt;code&gt;clearInterval()&lt;/code&gt; below after 5 seconds, that is when stop is printed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn more about concurrency in javascript and how can setTimeout(0) is used. &lt;br&gt;
🌟&lt;a href="https://www.youtube.com/watch?v=nqsPmuicJJc&amp;amp;t=1187s" rel="noopener noreferrer"&gt;setTimeout() by Akshay Saini&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
    <item>
      <title>Promises in javascript(🔖)</title>
      <dc:creator>Aishanii</dc:creator>
      <pubDate>Thu, 14 Jul 2022 07:24:43 +0000</pubDate>
      <link>https://forem.com/aishanipach/promises-in-javascript-4d6c</link>
      <guid>https://forem.com/aishanipach/promises-in-javascript-4d6c</guid>
      <description>&lt;p&gt;&lt;strong&gt;Promise is an object that bundles a provider and consumer.&lt;/strong&gt; To understand this, remember how this keyword was encountered in async javascript blog?&lt;/p&gt;

&lt;p&gt;Promises are used to keep track of whether a &lt;strong&gt;successful output was received&lt;/strong&gt; from a piece of code or not. &lt;/p&gt;

&lt;p&gt;Promises has two callbacks, either one of which is executed &lt;strong&gt;depending on the output received&lt;/strong&gt;-  resolve(value) &amp;amp; reject(error). &lt;/p&gt;

&lt;p&gt;Resolve is for &lt;em&gt;successful run&lt;/em&gt; while reject is for errors received.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyPromise&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rand&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rand&lt;/span&gt;&lt;span class="o"&gt;===&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;MyPromise&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Error&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)})&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2Ftx5037pcqxondo038t17.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ftx5037pcqxondo038t17.png" alt="Image description" width="800" height="186"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we are working with a &lt;a href="https://www.w3schools.com/jsref/jsref_random.asp" rel="noopener noreferrer"&gt;random number&lt;/a&gt; case, where it either returns 1 or 2. &lt;code&gt;.then&lt;/code&gt; handles succesful responses and &lt;code&gt;.catch&lt;/code&gt; works with errors. These are used to access internal Promise properties called &lt;strong&gt;&lt;code&gt;state&lt;/code&gt; and &lt;code&gt;result&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;state&lt;/strong&gt; indicates status of the job that needs to be done. Pending, fulfilled or rejected are the three indicators.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;result&lt;/strong&gt; is initially empty but gets a value when either resolved or an error when rejected.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Only the first call to resolve/reject is taken and once the value for state and result is set it's final. &lt;a href="https://javascript.info/promise-basics" rel="noopener noreferrer"&gt;More on promises by javascript.info&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;⭐New to Javascript? Try out great courses from &lt;a href="https://boot.dev/tracks/computer-science" rel="noopener noreferrer"&gt;https://boot.dev/tracks/computer-science&lt;/a&gt;&lt;/p&gt;

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