Mastering Async JavaScript: A Comprehensive Guide to Promises, Callbacks, and Async/Await

Mastering Async JavaScript: A Comprehensive Guide to Promises, Callbacks, and Async/Await

Description:→Unlock the power of asynchronous JavaScript with this guide. Learn how to use callbacks, promises, and async/await to build efficient, responsive web applications. Perfect for developers of all levels


1>Asynchronous JavaScript with Event Loop :→

so we will see it in 2 forms to understand the async nature of “JavaScript”.

1>Pictorial represent :→

a> why we need “async nature of js”?


2>How the “ASYNC JS“ works with “Event Loop” ?

2> By code :→

so 1st we start with “time functions” and let’s start with “settimeout()” function.

here is the syntax :→

setTimeout(() => {

}, timeout);

here is program to understand it better:→

function hello_fn(name) {
  return `hello ${name}!!!`;
}

setTimeout(() => {
  console.log(hello_fn("ayush"));
}, 2000);

here “2000” is in milisecond that means == 2sec.

it’s delaying already that’s why , it’s a async task.


2>Closures:→

here is the code part then we will the see the theory part :→

function out_fn(){
  let quality = "good boy"
  return function inner_fn(name) {
    return `${name} is as ${quality}`
  }
}
let ayush_69 = out_fn();// return the inner fxn
console.log(ayush_69("ayush"));

So, here we will discuss about the theory part of the Closure :→

what is “closure” ?

closure is just a function. when there is a function(outer) in that there is another function(inner) and the inner function can access outer function or variable.

how to access the “closure” ?

so if we execute the outter_fn. It will give me a function in return that we will save in variable(as function). we just have to execute that cz that will behave as inner_fn().