├── readme.md ├── Goodnotes 2.pdf ├── demo6.js ├── demo1.js ├── demo5.js ├── demo3.js ├── demo2.js ├── demo7.js └── demo4.js /readme.md: -------------------------------------------------------------------------------- 1 | Please donot touch me -------------------------------------------------------------------------------- /Goodnotes 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singhsanket143/Node_Js_Adv/HEAD/Goodnotes 2.pdf -------------------------------------------------------------------------------- /demo6.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.readFile('./readme.md', 'utf8', (err, data) => { // I/O queue - Callback queue 4 | if (err) { 5 | console.error(err); 6 | return; 7 | } 8 | console.log(data); 9 | }); 10 | 11 | setTimeout(() => console.log("Timer 1"), 0); 12 | -------------------------------------------------------------------------------- /demo1.js: -------------------------------------------------------------------------------- 1 | let a = 10; 2 | Promise.resolve().then(() => console.log("Printing from promise")); 3 | process.nextTick(() => console.log("printing from nextTick")); 4 | process.nextTick(() => console.log("printing from nextTick 1")); 5 | process.nextTick(() => console.log("printing from nextTick 2")); 6 | setTimeout(() => console.log("printing from timer"), 0); 7 | for(let i = 0; i < 10000000000; i++) {} 8 | console.log(a); -------------------------------------------------------------------------------- /demo5.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.readFile('./readme.md', 'utf8', (err, data) => { // I/O queue - Callback queue 4 | if (err) { 5 | console.error(err); 6 | return; 7 | } 8 | console.log(data); 9 | }); 10 | 11 | process.nextTick(() => console.log("Next tick")); 12 | Promise.resolve().then(() => console.log("Promise 1")); 13 | setTimeout(() => console.log("Timer 1"), 0); 14 | setImmediate(() => console.log("Immediate callback")); 15 | 16 | for(let i = 0 ; i < 10000000000; i++ ) {} // > 5s -------------------------------------------------------------------------------- /demo3.js: -------------------------------------------------------------------------------- 1 | console.log("Start"); 2 | 3 | setTimeout(() => { 4 | process.nextTick(() => console.log("Next Tick 2")); // cb will be waiting in nextTick queue 5 | }, 0); // cb will be waiting in timer queue 6 | 7 | Promise.resolve().then(() => console.log("Promise 1")); // cb will be waiting in promise queue 8 | 9 | process.nextTick(() => console.log("Next Tick 1")); // cb will be waiting in nextTick queue 10 | 11 | setTimeout(() => console.log("Timer 2"), 0); // cb will be waiting in timer queue 12 | 13 | 14 | console.log("end"); -------------------------------------------------------------------------------- /demo2.js: -------------------------------------------------------------------------------- 1 | console.log("Start"); 2 | 3 | setTimeout(() => console.log("Timer 1"), 0); // cb will be waiting in timer queue 4 | 5 | Promise.resolve().then(() => console.log("Promise 1")); // cb will be waiting in promise queue 6 | 7 | process.nextTick(() => console.log("Next Tick 1")); // cb will be waiting in nextTick queue 8 | 9 | setTimeout(() => console.log("Timer 2"), 0); // cb will be waiting in timer queue 10 | 11 | process.nextTick(() => console.log("Next Tick 2")); // cb will be waiting in nextTick queue 12 | 13 | for(let i = 0; i < 10000000000; i++) {} // by the time this loop completes all the callbacks are in their respective queues 14 | 15 | console.log("end"); -------------------------------------------------------------------------------- /demo7.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | 3 | fs.readFile('./readme.md', 'utf8', (err, data) => { // I/O queue - Callback queue 4 | if (err) { 5 | console.error(err); 6 | return; 7 | } 8 | process.nextTick(() => console.log("next tick from file read")); 9 | console.log(data); 10 | }); 11 | 12 | fs.readFile('./readme.md', 'utf8', (err, data) => { // I/O queue - Callback queue 13 | if (err) { 14 | console.error(err); 15 | return; 16 | } 17 | process.nextTick(() => console.log("next tick from file read 2")); 18 | console.log("2:", data); 19 | }); 20 | 21 | 22 | process.nextTick(() => console.log("Next tick")); 23 | Promise.resolve().then(() => console.log("Promise 1")); 24 | setTimeout(() => console.log("Timer 1"), 0); 25 | setImmediate(() => console.log("Immediate callback")); 26 | 27 | for(let i = 0 ; i < 10000000000; i++ ) {} // > 5s -------------------------------------------------------------------------------- /demo4.js: -------------------------------------------------------------------------------- 1 | console.log("Start"); 2 | 3 | setTimeout(() => { 4 | process.nextTick(() => console.log("Next Tick 2")); // cb will be waiting in nextTick queue 5 | process.nextTick(() => console.log("Next Tick 3")); 6 | process.nextTick(() => console.log("Next Tick 4")); 7 | Promise.resolve().then(() => console.log("Promise 2")); 8 | Promise.resolve().then(() => console.log("Promise 3")); 9 | Promise.resolve().then(() => console.log("Promise 4")); 10 | for(let i = 0; i < 10000000000; i++) {} 11 | 12 | }, 0); // cb will be waiting in timer queue 13 | 14 | Promise.resolve().then(() => console.log("Promise 1")); // cb will be waiting in promise queue 15 | 16 | process.nextTick(() => console.log("Next Tick 1")); // cb will be waiting in nextTick queue 17 | 18 | setTimeout(() => console.log("Timer 2"), 0); // cb will be waiting in timer queue 19 | 20 | 21 | console.log("end"); --------------------------------------------------------------------------------