Uncategorized

differance between setTimeOut and setImmediate with real scenario

Hi friends, after getting to many question what are differance between setTimeOut and setImmediate. when to use setTimeOut and setImmediate. real time example/ scenarion for setTimeOut and setImmediate. now i am trying to explain with simple example, might be helpful for you all.

setTimeout:

setTimeout(,0) essentially means execute after all current functions in the present queue get executed.

setImmediate:

setImmediate is similar in this regard except that it doesn’t use queue of functions. It checks queue of I/O eventhandlers. If all I/O events in the current snapshot are processed, it executes the callback.

Lets explain setImmediate and setImmediate differance with a scenario.

have a look below code.



console.log('GAME LOADING START');
const timeoutScheduled = Date.now();
// If game not loaded in 3 sec show message. 
let timeOutAfter3Sec = setTimeout(() => {
    console.log("it seems like you are offline check your internet connectivity/ might be compatiblity issue.");
clearImmediate(allisGood);
}, 3000); // After checking compatibility and internet connectivity, if everythings fine show this message. let allisGood=setImmediate(() => { console.log('Everythings are fine enjoy game'); }); // Code to check internet connectivity and compatibility. let doCheck = function () { // Check start, you can change 1.5e10 value to simulate both scenaion. set less value to call allisGood and more for timeOutAfter3Sec for (var i = 0; i < 1.5e2; i++) { //checking } // Check end //Now calculate delay time, //if less than 3 sec(2900ms), clearTimeOut For showing offline/compatibility, in this case setImmediate will call and display message //if more than 3 sec(2900ms), setTimeOut will fire from event loop que, and will clear clearImmediate. const delay = Date.now() - timeoutScheduled; if (delay < 2900) { clearTimeout(timeOutAfter3Sec); } console.log('loop end time',Date.now(),'delay',delay); } // call check function. doCheck();

i hope this line of code helpful for you, if enjoyed please share on social channel might be helpful for other. if have any doubt can ask/comment.

Uncategorized

java script callback vs promise vs async-await explanation with real time scenario

​Hi, Today i want to show how to use callback, convert call back code to promise and promise code to async await.

Scenario: i want to cook food and then want to east. to cook food we have to follow some steps and all steps have some time

  • Preparation of materials to cook food take some time for example 2000ms
  • frying them take some time for example 1000ms
  • Cooking them them take some time for example 3000ms
  • Finally want to EAT in 500ms

Let us see. Here written JS Functions for them. and now see the output for the same

/** 
 * Problem with Java Script
*/
let preparation=function(){
    setTimeout(()=>{
        console.log(‘preparation’);
    },2000)    
}

let frying=function(){
    setTimeout(()=>{
        console.log(‘frying’);
    },1000)    
}

let cooking=function(){
    setTimeout(()=>{
        console.log(‘Cooking’)
    },3000)
}

let eating=function(){
    setTimeout(()=>{
        console.log(‘Eating’)
    },500)
}

preparation();
frying();
cooking();
eating();

Output:

Eating
frying
preparation
Cooking

Wow Before cooking of food we are eating here..

Don’t worry, We have Solutions, Solutions 1: Call Back

/** 
 * Solution 1: Call Back
*/

let preparation=function(frying){
    setTimeout(()=>{
        console.log(‘preparation’);
        frying(cooking);
    },2000)    
}

let frying=function(cooking){
    setTimeout(()=>{
        console.log(‘frying’);
        cooking(eating);
    },1000)    
}

let cooking=function(eating){
    setTimeout(()=>{
        console.log(‘Cooking’);
        eating();
    },3000)
}

let eating=function(){
    setTimeout(()=>{
        console.log(‘Eating’)
    },500)
}

preparation(frying);

Output as expected

preparation
frying
Cooking
Eating

Don’t worry, We have Solutions, Solutions 2: Promise in Java Script

/** 
 * Solution2: Promise
*/

let preparation=new Promise( function(resolve, reject){
    setTimeout(()=>{        
        resolve(‘preparation=>’);      
    },1000)    
})

let frying=new Promise( function(resolve, reject){
    setTimeout(()=>{
        resolve(‘frying=>’);      
    },1000)    
})

let cooking=new Promise(function(resolve, reject){
    setTimeout(()=>{
        resolve(‘Cooking=>’);
    },3000)
})

let eating=new Promise( function(resolve, reject){
    setTimeout(()=>{
        resolve(‘Eating=>’);
    },500)
})

Promise.all([
   preparation,
   frying,
   cooking,
   eating 
]).then(a=>{
    console.log(…a);    
});

Output as expected

preparation
frying
Cooking
Eating

Don’t worry, We have Solutions, Solutions 3: Async Await

/** 
 * Solution3: Async Await
*/

let preparation=new Promise( function(resolve, reject){
    setTimeout(()=>{        
        resolve(‘preparation=>’);      
    },2000)    
})

let frying=new Promise( function(resolve, reject){
    setTimeout(()=>{
        resolve(‘frying=>’);      
    },1000)    
})

let cooking=new Promise(function(resolve, reject){
    setTimeout(()=>{
        resolve(‘Cooking=>’);
    },3000)
})

let eating=new Promise( function(resolve, reject){
    setTimeout(()=>{
        resolve(‘Eating=>’);
    },500)
})

let start=async function(){
 console.log(await preparation);
 console.log(await frying);
 console.log(await cooking);
 console.log(await eating);
}
start();

Output as expected

preparation
frying
Cooking
Eating

if enjoyed please like and share with other, might be helpful for them