Skip to content

Commit

Permalink
Ep-17 | TRUST ISSUES with setTimeOut()
Browse files Browse the repository at this point in the history
  • Loading branch information
deep100x committed Jun 21, 2024
1 parent 0fb475f commit 95abb75
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Namaste-JavaScript/Ep-16-JS-Engine-V8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// JS runs literally everywhere from smart watch to robots to browsers because of Javascript Runtime Environment JRE.
// Itʼs all because of Javascript runtime Environment JRE

// JRE container has All the things required to runs javascript code.
// JRE is not possible without
// Js Engine Heart of JRE

// JS creator Brendan Eich), v8 Chrome


// Code inside Javascript Engine passes through 3 steps:
// 1. Parsing
// 2. Compilation
// 3. Execution
43 changes: 43 additions & 0 deletions Namaste-JavaScript/Ep-17-SetTimeOut-Trust-Issues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function cb () {
console.log("Callback");
}

setTimeout(cb,5000); // Does'nt take exactly 5000 ms but it guarantees that it takes atleast 5000 ms And maybe Anytime after that...

// Comment clarified:
// setTimeout does not guarantee that the callback method will be called exactly after 5 seconds.
// It might take slightly longer if the call stack is busy.

console.log("Start");
setTimeout(function cb () {
console.log("Callback");
},5000);
console.log("End");

let startDate = new Date().getTime();
let endDate = startDate;
while (endDate < startDate + 10000)
{
endDate = new Date().getTime();
}
console.log("while expires");


// Why JS has only one callsatck ?
// Synchronous single threaded language
// it has just 1 main thread and 1 call satck
// all the pieces of code executed there inside
// it makes interpreted language and it runs very fast even inside browser

// you dont have to wait for compiled just like languages
// it hase just in time compilation

// we can still do asynchronous operations


setTimeout(function deep () {
console.log("-------- Less Important... it Might be on 4th page or not at priority --------");
},0)

// In short Defer some piece of code
// After completed execution of whole thread it goes into callback queue and then callstack run that function

0 comments on commit 95abb75

Please sign in to comment.