Learnitweb

Author: Editorial Team

  • Asynchronous task priorities and task queue

    In the previous lessons, we explored different ways to write asynchronous code in JavaScript, such as: All these approaches help us deal with tasks that do not complete immediately, such as API calls, timers, or user interactions. However, an important question often remains unanswered: In what order are these asynchronous operations actually executed? More specifically:…

  • Understanding Scalars and Vectors

    Introduction Linear Algebra forms the mathematical backbone of data science, machine learning, artificial intelligence, and many modern computational systems.Whenever we work with datasets, train models, or visualize information, we are implicitly working with mathematical structures such as scalars, vectors, and matrices. In this tutorial, we will focus on two foundational concepts: scalars and vectors, which…

  • async in JavaScript

    According to the definition, async is a keyword that allows an asynchronous function to be written in a way that looks synchronous. But let’s not focus on definitions too much.Instead, let’s understand what actually happens. When you place the async keyword before a function, JavaScript automatically makes that function return a promise. Even if you…

  • Promise.any()

    Revisiting the problem with Promise.race() Suppose Eugene answers in 1 second, but he doesn’t have a pen. John and Susie respond later, but they do have pens. Since Promise.race() reacts to the first settled promise, the final result becomes a rejection. Here’s how that situation looks in code: Output: Even though Susie could help, Promise.race()…

  • Understanding Promise.race() in JavaScript

    The Core Idea of Promise.race() Promise.race(): This means: Real-Life Example: Borrowing a Pen at School Let’s understand this with a simple real-world example. Imagine: Each one replies at a different speed. Their response times: Friend Response Time Has a Pen? John 3 seconds Yes Eugene 5 seconds No Susie 2 seconds Yes You don’t want…

  • Understanding Promise.allSettled() in JavaScript

    In the previous lessons, we discussed Promise.all() and how it works when executing multiple asynchronous operations in parallel. You also saw an important limitation: If any one promise rejects, Promise.all() immediately rejects and stops waiting for the remaining promises. In this tutorial, we’ll explore a safer alternative — Promise.allSettled() This method allows you to run…

  • Executing Multiple Promises in Parallel Using Promise.all()

    In many real-world applications, we often need to execute multiple asynchronous operations at the same time and wait for all of them to finish before proceeding. A very common example of this is when an application needs to make multiple HTTP requests in parallel — such as fetching data from different services — and only…

  • Converting Values to Promises in JavaScript

    Understanding Promise.resolve() and Promise.reject() In this tutorial, we are going to understand two very important JavaScript methods that help us convert any value into a Promise: These methods are extremely useful when working with existing code that expects Promises, but sometimes you only have normal (non-promise) values. Why Do We Need This? In real-world JavaScript…

  • Handling Promise Rejections Using .then() and .catch()

    Using .then() with One or Two Arguments The .then() method can accept two arguments: So far, we mostly used .then() with only one argument, which handles successful cases. Example: Handling Both Success and Failure Let’s use our familiar calculateSquare function. This works correctly: However, this approach has a limitation. The Hidden Problem Let’s introduce an…

  • Chaining Promises Using .then()

    The Key Idea: .then() Returns a New Promise This is extremely important: Every .then() call returns a new promise. Because of this: This allows us to create a chain of asynchronous operations. Step 1: Basic Promise Chain Let’s start with a simple example. Output: So far, nothing new. Step 2: Adding Another .then() Now let’s…