<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tofail Ahmed Sayem </title>
    <description>The latest articles on DEV Community by Tofail Ahmed Sayem  (@tofail).</description>
    <link>https://dev.to/tofail</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%2F1237564%2F40fb6b0d-1c10-461d-a187-8b4fa9ff8b5c.jpg</url>
      <title>DEV Community: Tofail Ahmed Sayem </title>
      <link>https://dev.to/tofail</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tofail"/>
    <language>en</language>
    <item>
      <title>Node.js Asynchronous Architecture</title>
      <dc:creator>Tofail Ahmed Sayem </dc:creator>
      <pubDate>Sun, 30 Jun 2024 08:48:05 +0000</pubDate>
      <link>https://dev.to/tofail/how-nodejs-handl-multiple-task-being-single-threaded-3lh7</link>
      <guid>https://dev.to/tofail/how-nodejs-handl-multiple-task-being-single-threaded-3lh7</guid>
      <description>&lt;p&gt;Node.js is single threaded and operates asynchronously using the "Single Threaded Event Loop" architecture and handles multiple concurrent clients efficiently without multithreading. Node.js is actually designed to build distributed applications with many nodes and that's the reason why it's called Node. Actually, the main event loop is single threaded but Node.js uses multiple threads in the background to execute asynchronous code.&lt;/p&gt;

&lt;p&gt;Node.js is non-blocking which means that all the callbacks or functions are delegated to the event loop and are executed by different threads. This means that when other processes are executing in a concurrent manner, the event loop continues to run as non-JavaScript operations. This way the tasks are executed asynchronously.&lt;/p&gt;

&lt;p&gt;Needless to say that Node.js is extremely good at handling multiple requests concurrently using a single thread model. This is called "asynchronous cooperative multitasking".&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Management of asynchronous code in Node.js:&lt;/strong&gt;&lt;br&gt;
A lot of times we have dealt with multithreading and preemptive multitasking scenarios but the first time we encountered cooperative multitasking using Node.js was when creating a User Module at the earlier stage of our product creation.&lt;/p&gt;

&lt;p&gt;This module deals with managing different user roles and permissions and consolidates the respective entitlement of users of different levels (like Admin and User). We understood that asynchronous code in Node.js can be handled in 3 possible ways:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.Callbacks:&lt;/strong&gt; Callback is an executable code that is passed as an argument to another code and is expected to execute (or call back) at a specific time. Callbacks make sure that certain code doesn't execute until another code is finished its execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.Using Async/Await with Promises:&lt;/strong&gt; The async/await pattern structures an asynchronous, non-blocking function similar to a synchronous function. It provides opportunities to the program to execute other code while waiting for a long-running asynchronous task to complete. The await keyword within the async function returns a promise value that ensures the current function is completed before calling the subsequent function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.Using Promises:&lt;/strong&gt; Promises are abstractions provided that give us a way to express ourselves synchronously when writing asynchronous code. A promise returns a value that is either resolved or the reason why that's not resolved (for example, when a network error occurs). &lt;/p&gt;

&lt;p&gt;A promise can be in any one of the following states:&lt;br&gt;
&lt;strong&gt;States of a Promise&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Pending:&lt;/strong&gt; async operation is incomplete and promise does not have a value yet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fullfilled:&lt;/strong&gt; async operation is completed and promise has a value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rejected:&lt;/strong&gt; async operation has failed and promise will never get fulfilled.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Typescript over JavaScript</title>
      <dc:creator>Tofail Ahmed Sayem </dc:creator>
      <pubDate>Sun, 30 Jun 2024 03:36:55 +0000</pubDate>
      <link>https://dev.to/tofail/typescript-over-javascript-3oji</link>
      <guid>https://dev.to/tofail/typescript-over-javascript-3oji</guid>
      <description>&lt;p&gt;Typescript is new javascript with some advanced and useful features added to javascript. Typescript, which is the superset of javascript and becoming a go to language in the web development sector for every large application. The feature of detecting bugs while typing the code makes it unique. As per research, 60% of javascript developers started using typescript. &lt;/p&gt;

&lt;p&gt;Providing syntax for types is the key feature of Typescript with its name implemented as ‘TYPESCRIPT". Even tech giants companies like Microsoft are migrating to typescript for its functionalities and features. Communicating  tools like Slack and Asana use typescript for their implementation. Developers who work on Angular, React, or Vue use Typescript. For better productivity, Typescript is better for its better collaboration. So, when there is so much about typescript, we should know why we should pick typescript over javascript.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Points to be noted: *&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A .ts extension is used to save a Typescript file.&lt;/li&gt;
&lt;li&gt;A javascript file saved with a .ts extension works perfectly as a typescript file.&lt;/li&gt;
&lt;li&gt; Typescript is preferred as a server side programming language.&lt;/li&gt;
&lt;li&gt; Typescript is preferable for building large scale applications.&lt;/li&gt;
&lt;li&gt;It is statically typed i.e. unless you give the type of the variable, the code will not run which makes error detection easy.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why should we pick typescript over javascript? Let's discuss some points.&lt;/p&gt;

&lt;p&gt;1) &lt;strong&gt;Best for creating large scale application:&lt;/strong&gt; &lt;br&gt;
Typescript is considered as the best programming language for developing large scale or enterprise level applications;. Due to its ‘type’ feature it's easy to understand for developers to read, which is not the case of Javascript. Also its feature of detecting bugs while typing makes it less time consuming. For large projects, typescript helps by catching mistakes with its type system which eventually helps to handly develop and maintain projects with less bugs.&lt;/p&gt;

&lt;p&gt;2) &lt;strong&gt;Classes behave same as C++ , python:&lt;/strong&gt;&lt;br&gt;
If someone has an idea of C++, Python, then he/she should know that Typescript behaves the same way.  Unlike Javascript, Typescript behaves the same, especially ‘this’ keyword and defining method. Typescript supports the OOPS concept and also helps in server-side development.&lt;/p&gt;

&lt;p&gt;3) &lt;strong&gt;Framework encourages Typescript:&lt;/strong&gt;&lt;br&gt;
These days various frameworks like React, Angular, NestJS, VueJS support Typescript. Even Angular has its primary language as Typescript. Typescript is javascript with extra features and supports ECMAScript. All javascript frameworks are configurable in working with typescript because of its statically typed and auto bug detecting while coding or development. Implementing the OOPS concept becomes easy with typescript which gives the reason for using typescript over javascript.&lt;/p&gt;

&lt;p&gt;4) &lt;strong&gt;Type Hinting/TypeCasting:&lt;/strong&gt;&lt;br&gt;
Typescript follows TypeCasting which brings the reason to use it over Javascript. To resolve the query in no time , it shows the error while coding. With TypeCasting it's easy to convert one variable type to another. TypeCasting allows variable type as per as requirement then and there. Using the ‘as’ keyword or &amp;lt;&amp;gt; operator is suitable for TypeCasting.  At the time of development, Typescript displays the compilation error. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5) Promotes a Better Development Experience:&lt;/strong&gt;&lt;br&gt;
Typescript is very easy to understand and re-write, it becomes easy for developers to work even after gaps. Every line has a comment feature where coders can type comments while coding, for future use. It happens that more than one developer is working together on one application, that can make a mess but with the type feature it can be easy to debug. This also brings better opportunities  for collaboration because any developer can easi;y understand the codebases and start making changes as requirements. This, in return, promotes a better development experience and also could return efficient output. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6) Targets multiple browsers:&lt;/strong&gt;&lt;br&gt;
It’s better to choose a language that is compatible with all browsers, rather than checking the application on browsers one by one. Once code writing is done and Typescript takes care about which browser should be targeted. It follows the process of compiling the Typescript to Javascript and then runs on browsers. So eventually it supports all browsers which support javascript. This is the biggest reason why anyone should choose Typescript over Javascript.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to make interfaces optional in typescript</title>
      <dc:creator>Tofail Ahmed Sayem </dc:creator>
      <pubDate>Sat, 29 Jun 2024 17:45:13 +0000</pubDate>
      <link>https://dev.to/tofail/optional-interfaces-112j</link>
      <guid>https://dev.to/tofail/optional-interfaces-112j</guid>
      <description>&lt;p&gt;First of all let us look at some random example,&lt;br&gt;
&lt;/p&gt;

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

interface personMale{
      gender:"male";
      salary:number;
}
interface personFemale{
      gender:"female";
      weight:number
}
type person={
      name:string;
      age:number;


}&amp;amp;(personMale|personFemale)


const person1:person={
      name:"Sayem",
      age:27,
      gender:"male",
      salary:0
}
const person2:person={
      name:"Setara",
      age:24,
      gender:"female",
      weight:55
}

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

&lt;/div&gt;



&lt;p&gt;Here we should use "male" and "salary" together and "female" and " "weight" together. If we want to use "male" and "weight" or "female" and "salary" together, it will through error.&lt;/p&gt;

&lt;p&gt;In Typescript, we can define optional properties in an interface by adding a question mark (?) to the property name. This tells Typescript that this property may or may not exist on the object.&lt;/p&gt;

&lt;p&gt;As mentioned earlier, the basic way to make a property optional is by appending a question mark(?) to the property name. Here is an simple example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface User {
  id: number;
  name?: string;
  email?: string;
}


let user: User = { id: 1 };

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

&lt;/div&gt;



&lt;p&gt;In the above example, name and email are optional. If we create an object named ‘user’ with only ‘id’ property Typescript will not complain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using utility type:&lt;/strong&gt;&lt;br&gt;
Typescript provides several utility types to manipulate types, another being Partial, which makes all properties in a type T optional. Here’s how we can use it,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface User {
  id: number;
  name: string;
  email: string;
}


type OptionalUser = Partial&amp;lt;User&amp;gt;;


let user: OptionalUser = { id: 1 };

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

&lt;/div&gt;



&lt;p&gt;In the above example, OptionalUser is a new type where all properties of User are optional. Hence, we can assign an object with only the id property to the user.&lt;/p&gt;

</description>
      <category>interface</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Typescript Generics</title>
      <dc:creator>Tofail Ahmed Sayem </dc:creator>
      <pubDate>Sat, 29 Jun 2024 16:12:24 +0000</pubDate>
      <link>https://dev.to/tofail/typescript-generics-1ki2</link>
      <guid>https://dev.to/tofail/typescript-generics-1ki2</guid>
      <description>&lt;p&gt;Generics in Typescript are a feature that allows you to create components that can work with any data type while preserving type safety.  They provide a way to define functions, classes and interfaces without specifying the exact data types they will operate on in advance. Instead, you use type variables, usually represented by letters like , to create a placeholder that will be replaced with a specific type when the component is used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function identity&amp;lt;T&amp;gt;(arg: T): T {
    return arg;
}


let numberIdentity = identity&amp;lt;number&amp;gt;(42); // Works with numbers
let stringIdentity = identity&amp;lt;string&amp;gt;("Hello"); // Works with strings

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

&lt;/div&gt;



&lt;p&gt;Let's consider 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 printData(data: number) {
    console.log("data: ", data);
}


printData(2);

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

&lt;/div&gt;



&lt;p&gt;In this example, we can not assign any string or object or boolean or array as the value of  ‘data’. Instead of previous example, we can rewrite the example as below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printData(data: number | string | boolean) {
    console.log("data: ", data);
}


printData(2);
printData("hello");
printData(true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example is a better approach than the previous one. In this example we can assign an argument value either number or string or boolean. But incase of complex argument or multiple argument it will be a little awkward looking and complicated. Instead of last example we can try another approach,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printData&amp;lt;T&amp;gt;(data: T) {
    console.log("data: ", data);
}


printData(2);
printData("hello");
printData(true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example we can pass any type of data as the value of argument ‘data’. We can pass string, boolean, number, obj, array without mentioning data type. &lt;/p&gt;

&lt;p&gt;How Generics work is TS:&lt;br&gt;
Generics are like variables - to be precise , type variables - that store the type(for example number, string, boolean) as a value.&lt;/p&gt;

&lt;p&gt;So, we can discuss generics as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printData&amp;lt;T&amp;gt;(data: T) {
    console.log("data: ", data);
}


printData(2);
printData("hello");
printData(true);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above example,&lt;br&gt;
1) We can use type variable inside angular brackets after the fn name &lt;br&gt;
2) Then assign the type variable to the parameter data:T&lt;/p&gt;

&lt;p&gt;Let explore bit more-&lt;br&gt;
To use generics , we need to use angular brackets and then specify a type variable inside them. Developers generally use T, X, Y, V etc etc. But it can be anything depending upon preferences.&lt;br&gt;
Even if we can pass an array of something or an object of something as an argument of data value, everything will be displayed without TS complaining.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printData&amp;lt;T&amp;gt;(data: T) {
    console.log("data: ", data);
}


printData(2);
printData("hello");
printData(true);
printData([1, 2, 3, 4, 5, 6]);
printData([1, 2, 3, "hi"]);
printData({ name: "Ram", rollNo: 1 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let  see another 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 printData&amp;lt;X,Y&amp;gt;(data1: X, data2: Y) {
    console.log("Output is: ", data1, data2);
}


printData("Hello", "World");
printData(123, ["Hi", 123]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example we passed two arguments to fn and used X and Y as type denotion for both parameters. X refers to the first value of the argument and Y refers to the second value of the arguments.&lt;br&gt;
Here as well, the type of data1 and data2 are not specified explicitly because TS handles the type inference with the help of generics. &lt;/p&gt;

</description>
      <category>typescript</category>
      <category>generics</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Event loop in Node.js</title>
      <dc:creator>Tofail Ahmed Sayem </dc:creator>
      <pubDate>Tue, 25 Jun 2024 06:26:45 +0000</pubDate>
      <link>https://dev.to/tofail/event-loop-in-nodejs-eoa</link>
      <guid>https://dev.to/tofail/event-loop-in-nodejs-eoa</guid>
      <description>&lt;p&gt;The Node.js event loop is a critical concept for understanding how Node.js manages asynchronous operations and ensures efficient handling of concurrent tasks without blocking. Here's a breakdown of its main components and how it functions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Loop:&lt;/strong&gt; The event loop is the central mechanism that manages all asynchronous operations in Node.js. It continuously checks for events in the event queue and processes them in a loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Queue:&lt;/strong&gt; The event queue holds various types of events such as callbacks, timers, and I/O events. These events are queued up when asynchronous operations are initiated and are processed sequentially by the event loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callbacks:&lt;/strong&gt; Callbacks are functions passed as arguments to be executed later, typically after a specific operation or event completes. They are essential for handling asynchronous results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Timers:&lt;/strong&gt; Node.js provides timers like setTimeout() and setInterval() to schedule callbacks to execute after a specified delay or at regular intervals. These timers are managed by the event loop to trigger their associated callbacks at the appropriate times.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I/O Operations:&lt;/strong&gt; When Node.js performs I/O operations (e.g., file system operations, network requests), it delegates these tasks to the operating system and registers callbacks. Once the operations are completed, their respective callbacks are queued in the event loop for execution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Process Overview:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Check:&lt;/strong&gt; The event loop continuously checks the event queue for pending events.&lt;br&gt;
&lt;strong&gt;Event Execution:&lt;/strong&gt; If events are found in the queue, the event loop dequeues and executes their associated callbacks.&lt;br&gt;
&lt;strong&gt;Microtasks:&lt;/strong&gt; After executing regular callbacks, the event loop processes any microtasks queued in the microtask queue. Microtasks typically include promises and other high-priority tasks.&lt;br&gt;
&lt;strong&gt;I/O and Timers:&lt;/strong&gt; The event loop handles ready I/O operations and checks if any scheduled timers have expired, executing their callbacks as necessary.&lt;br&gt;
&lt;strong&gt;Repeat:&lt;/strong&gt; The process repeats itself, ensuring continuous handling of asynchronous events without blocking the application.&lt;br&gt;
Concurrency and Blocking:&lt;/p&gt;

&lt;p&gt;The event loop enables Node.js to manage multiple tasks concurrently within a single thread, making it highly efficient for I/O-bound operations.&lt;br&gt;
CPU-bound operations can potentially block the event loop and should be handled separately using worker threads or other processes to maintain application responsiveness.&lt;br&gt;
Understanding the event loop is fundamental for developing efficient Node.js applications, ensuring optimal performance under varying workloads.&lt;/p&gt;

</description>
      <category>eventloop</category>
      <category>node</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
