Monday, 24 April 2017

Node.js - Global Objects

Node.js - Global Objects

Question: How to display the current filename?
console.log(__filename);
This gives you local filename of the current module.



Question: How to display the current directory name?
console.log(__dirname);
This gives you local dirname of the current module.



Question: How to call a function after 5 seconds?
For this, you can use a global function i.e setTimeout.
function helloFunction(){
   console.log( "Hello, World!");
}
/* helloFunction will call after 5 seconds*/
setTimeout(helloFunction, 5000);



Question: How to stop calling a function which was started with setTimeout?
For this, you can use a global function i.e clearTimeout.
function helloFunction(){
   console.log( "Hello, World!");
}
/* helloFunction will call after 5 seconds*/
var funcObj=setTimeout(helloFunction, 5000);

//stop funtion
clearTimeout(funcObj);



Question: How to call a function in every 7 seconds?
For this, you can use a global function i.e setInterval.
function helloFunction(){
   console.log( "Hello, World!");
}
/* helloFunction will call in every 7 seconds*/
setInterval(helloFunction, 7000);



Question: How to stop calling a function which was started with setInterval?
For this, you can use a global function i.e clearInterval
clearInterval(funcObj);



Question: What are different Console Methods to print info, error, warning etc?
  1. console.log: Prints to stdout with newline.
  2. console.error: Prints to stderr with newline.
  3. console.dir: Uses util.inspect on obj and prints resulting string to stdout.
  4. console.time: Mark the time
  5. console.timeEnd: Mark the time end
  6. console.trace: Print to stderr Trace



Question: What are Process Events?
The process object is a global object and can be accessed from anywhere.
process.on('exit', function(code) {   
   console.log('About to exit with code:', code);
});
console.log("Program Ended");



Question: What are different the Process Events?
  1. exit
  2. beforeExit
  3. uncaughtException
  4. Signal Events


No comments :

Post a Comment