<?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: 3BiEiD</title>
    <description>The latest articles on DEV Community by 3BiEiD (@abdomohamed785).</description>
    <link>https://dev.to/abdomohamed785</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%2F508912%2Fe778b667-cae7-484e-aff6-37f47859cb0b.jpeg</url>
      <title>DEV Community: 3BiEiD</title>
      <link>https://dev.to/abdomohamed785</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdomohamed785"/>
    <language>en</language>
    <item>
      <title>Methods of Handling Asynchronous Code</title>
      <dc:creator>3BiEiD</dc:creator>
      <pubDate>Fri, 22 Dec 2023 13:59:58 +0000</pubDate>
      <link>https://dev.to/abdomohamed785/methods-of-handling-asynchronous-code-3e98</link>
      <guid>https://dev.to/abdomohamed785/methods-of-handling-asynchronous-code-3e98</guid>
      <description>&lt;p&gt;JavaScript provides three methods of handling asynchronous code: callbacks, promises, and async/await. Let's discuss each.&lt;/p&gt;

&lt;h3&gt;
  
  
  Callbacks
&lt;/h3&gt;

&lt;p&gt;Asynchronous callbacks are functions that are passed to another function so that the internal function can begin running code in the background. As soon as we need to handle multiple asynchronous operations, Callback Functions nest into one another, which leads to callback hell. This makes callbacks an old-fashioned method of writing asynchronous Javascript.&lt;/p&gt;

&lt;p&gt;This is the basic syntax of the callback function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function demoFunction(callback){
  callback();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Promises
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://dev.to/abdomohamed785/promises-in-js-i87"&gt;Promises&lt;/a&gt; are used to track asynchronous operations, whether the asynchronous event has been executed or not. Promises have four states:&lt;br&gt;
&lt;strong&gt;Pending&lt;/strong&gt;: The initial state of promise before the event happens not fulfilled or rejected yet.&lt;br&gt;
&lt;strong&gt;Fulfilled&lt;/strong&gt;: Action/Operation related to the promise completed successfully.&lt;br&gt;
&lt;strong&gt;Rejected&lt;/strong&gt;: Action/Operation related to the promise failed&lt;br&gt;
&lt;strong&gt;Settled&lt;/strong&gt;: Promise has been fulfilled or rejected&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let promise = new Promise (function(resolve, reject) {
  ... code
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Async/Await
&lt;/h3&gt;

&lt;p&gt;Async/await works with promises in asynchronous functions, making asynchronous code appear more like synchronous or procedural code. A promise is returned by an asynchronous function.&lt;/p&gt;

&lt;p&gt;The syntax of an async function is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function name(parameter1, parameter2, ...paramaterN) {
  // statements
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Await can only be used in async functions, and it is used for calling async functions. Await blocks the execution of the code inside the async function in which it is contained until the async function has either resolved or been rejected.&lt;/p&gt;

&lt;p&gt;Here is the syntax of the await function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let result = await promise;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>abdulrhmanjs</category>
    </item>
    <item>
      <title>Promises in Js</title>
      <dc:creator>3BiEiD</dc:creator>
      <pubDate>Fri, 22 Dec 2023 13:37:56 +0000</pubDate>
      <link>https://dev.to/abdomohamed785/promises-in-js-i87</link>
      <guid>https://dev.to/abdomohamed785/promises-in-js-i87</guid>
      <description>&lt;p&gt;JavaScript Promise are easy to manage when dealing with multiple asynchronous operations where callbacks can create callback hell leading to unmanageable code. &lt;/p&gt;

&lt;p&gt;Prior to promises &lt;strong&gt;events&lt;/strong&gt; and &lt;strong&gt;callback&lt;/strong&gt; functions were used but they had limited functionalities and created unmanageable code. &lt;/p&gt;

&lt;p&gt;Multiple callback functions would create callback hell (a chain of limitless callbacks inside each other) that leads to unmanageable code. &lt;/p&gt;

&lt;p&gt;Promises are used to handle asynchronous operations in JavaScript.&lt;/p&gt;

&lt;p&gt;Promises are objects that have four states :&lt;br&gt;
1) &lt;strong&gt;Pending&lt;/strong&gt;: The initial state which means not rejected nor resolved. &lt;br&gt;
2) &lt;strong&gt;Fulfilled&lt;/strong&gt;: This term refers to successful completion. &lt;br&gt;
3) &lt;strong&gt;Rejected&lt;/strong&gt;:  means the promise cannot be fulfilled due to an error or some exceptional situations in asynchronous operations. &lt;br&gt;
4) &lt;strong&gt;Settled&lt;/strong&gt;: Promise has been fulfilled or rejected&lt;/p&gt;

&lt;p&gt;You can decide what to do upon this promise either in the happy way or the errors way, so there are 2 methods to do so &lt;br&gt;
then() and catch()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let promise = new Promise(function (resolve, reject) {
    const x = "test";
    const y = "test"
    if (x === y) {
        resolve();
    } else {
        reject();
    }
});

promise
    .then(function () {
        console.log('Success');
    })
    .catch(function () {
        console.log('Some error has occurred');
    });

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

&lt;/div&gt;



&lt;p&gt;then() method can take 2nd argument, so you ignore catch()&lt;br&gt;
 The second argument is executed if the promise is rejected and an error is received. (It is optional and there is a better way to handle error using .catch() method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let promise = new Promise(function (resolve, reject) {
    resolve('test again');
})

promise
    .then(function (successMessage) {
        //success handler function is invoked 
        console.log(successMessage);
    }, function (errorMessage) {
        console.log(errorMessage);
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>abdulrhmanjs</category>
    </item>
    <item>
      <title>Sync VS Async in Js</title>
      <dc:creator>3BiEiD</dc:creator>
      <pubDate>Fri, 22 Dec 2023 07:53:20 +0000</pubDate>
      <link>https://dev.to/abdomohamed785/sync-vs-async-in-js-40c7</link>
      <guid>https://dev.to/abdomohamed785/sync-vs-async-in-js-40c7</guid>
      <description>&lt;h2&gt;
  
  
  Synchronization in JavaScript?
&lt;/h2&gt;

&lt;p&gt;JavaScript is synchronous by default: every line of code is executed one after the other, and each task must wait for the previous one to be completed before moving to the next.&lt;/p&gt;

&lt;p&gt;Let’s look at this illustration below to see how synchronous JavaScript code runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('One');
console.log('Two');
console.log('Three');
// LOGS: 'One', 'Two', 'Three'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, the first line of code, One, will be logged first, followed by the second line, Two, and the third line, Three. It’s easy to see that the code works sequentially; each line of code waits for the former to be completed before it executes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Synchronous programming can be used when the aim is for simplicity rather than efficiency.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  (A)Synchronization in JavaScript?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Asynchronous JavaScript programming is one of the key components of the language because it controls how we carry out tasks that require a lot of time&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asynchronous programming is programming that allows multiple events to occur simultaneously. This means that one operation may take place while another is still being processed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It allows operations to take place in a non-sequential manner. Many web API features now require asynchronous code this is true for those that access or fetch data from external sources.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An example includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;retrieving files from databases&lt;/li&gt;
&lt;li&gt;accessing a video stream from a webcam&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Because async is multi-threaded, it makes code non-blocking and allows tasks to complete quickly so that the other functions can continue to operate as the request is being processed. In a nutshell, asynchronous code starts now and finishes later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('One');
setTimeout(() =&amp;gt; console.log('Two'), 100);
console.log('Three');

// LOGS: 'One', 'Three', 'Two'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The setTimeout is what makes our code &lt;strong&gt;asynchronous&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;What the code does first in the example above is to log One. Then, instead of executing the setTimeout function, it logs Three before it runs the setTimeout function. Browsers run JavaScript, and there are web APIs that handle it for users.&lt;/p&gt;

&lt;p&gt;JavaScript passes the setTimeout function in these web APIs, and then our code keeps running normally. &lt;/p&gt;

&lt;p&gt;By running code asynchronously, other code is not blocked from running (non-blocking). When the work is complete, a notification is sent to the main thread about whether the task was successful or failed.&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;1)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Async is multi-thread, which means operations or programs can run in parallel.&lt;/li&gt;
&lt;li&gt;Sync is a single-thread, so only one operation or program will run at a time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Async is non-blocking, which means it will send multiple requests to a server.&lt;/li&gt;
&lt;li&gt;Sync is blocking — it will only send the server one request at a time and wait for that request to be answered by the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Async increases throughput because multiple operations can run at the same time.&lt;/li&gt;
&lt;li&gt;Sync is slower and more methodical.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>abdulrhmanjs</category>
    </item>
    <item>
      <title>A debate about x++; ⁉️🤔</title>
      <dc:creator>3BiEiD</dc:creator>
      <pubDate>Tue, 07 Nov 2023 19:23:06 +0000</pubDate>
      <link>https://dev.to/abdomohamed785/a-debate-about-x--1led</link>
      <guid>https://dev.to/abdomohamed785/a-debate-about-x--1led</guid>
      <description>&lt;p&gt;Hi there, I hope you all are fine&lt;br&gt;
let us talk a little bit about C++ programming language and take a deep dive into assignment operator (=) and post-increment operator (++)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;
int main()
{
    int x = 5;
    x = x++;
    cout&amp;lt;&amp;lt;x;    // 5
    return 0;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The question is why x equals 5?&lt;br&gt;
First of all, let us divide this statement into 3 parts&lt;br&gt;
1) x&lt;br&gt;
2) =&lt;br&gt;
3) x++;&lt;br&gt;
As the assignment operator = assigns the right side to the left side and returns the left side we should start with the right-most part which is x++;&lt;/p&gt;

&lt;p&gt;x++ This is a post-increment operator which means it will evaluate the current value (5), and increment the value in memory for the upcoming operations (6).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you use x++, the value of x is first used in the expression where x++ appears, and then x is incremented by 1.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;until now everything is clear?&lt;br&gt;
again&lt;br&gt;
so x++; will do two things, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;it'll give you a temporary value (we can say) which is 5, and,&lt;br&gt;
&lt;strong&gt;change x's real value in memory&lt;/strong&gt; by 1.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;but, if we say x = 900; we obviously say in this statement:&lt;br&gt;
&lt;strong&gt;change x's real value in memor&lt;/strong&gt;y and assign 900 to it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If you notice same bold phrase repeated which means the same behavior in 2 different situations normal assignment and post-increment.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;so finally the x = part will overwrite (x = 6 in memory) so it will remain 5.&lt;br&gt;
I hope you got it 👍&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
