Best Online Tutorials

SQL Tutorial

Free SQL Tutorial

SQL Interview Questions

SQL Practice Questions

SQL Tips & Tricks

SQL Errors

SQL Books

Angular Tutorial

Free Angular Tutorial

Angular Interview Questions

Angular Practice Questions

Angular Tips & Tricks

Angular Errors

Angular Books

Node.js Tutorial

Free Node.js Tutorial

Node.js Interview Questions

Node.js Practice Questions

Node.js Tips & Tricks

Node.js Errors

Node.js Books

React.js Tutorial

Free React.js Tutorial

React.js Interview Questions

React.js Practice Questions

React.js Tips & Tricks

React.js Errors

React.js Books

SSIS Tutorial

Free SSIS Tutorial

SSIS Interview Questions

SSIS Practice Questions

SSIS Tips & Tricks

SSIS Errors

SSIS Books

SEO Tutorial

Free SEO Tutorial

SEO Interview Questions

SEO Practice Questions

SEO Tips & Tricks

SEO Errors

SEO Books

Javascript Tutorial

Free Javascript Tutorial

Javascript Interview Questions

Javascript Practice Questions

Javascript Tips & Tricks

Javascript Errors

Javascript Books

MongoDB Tutorial

Free MongoDB Tutorial

MongoDB Interview Questions

MongoDB Practice Questions

MongoDB Tips & Tricks

MongoDB Errors

MongoDB Books

Next.js Tutorial

Free Next.js Tutorial

Next.js Interview Questions

Next.js Practice Questions

Next.js Tips & Tricks

Next.js Errors

Next.js Books

Node.js Interview Questions and Answers | Part 7 

Prashant Surya  |   Jan 3, 2020  |  Views: 202
pictures

Description:

1. What is tick in Nodejs ?


tick means iteration, A iteration of event loop.

since its a "loop" , it means "the next time it loops", so a tick its a whole loop , it ends when no events are triggered and nodejs has looped all to check if any is triggered, 

"nextTick" it means the next loop after the current one. 


2. How process.nextTick() works in Node.js ?


process.nextTick() is used to schedule a callback function to be invoked in the next iteration of the Event Loop. 
process.nextTick(() => {
  //do something
})


3. Difference between process.nextTick() and setTimeout() ?


process.nextTick()
process.nextTick() function is specific to the Node.js Event Loop.
It takes only callback as input.
It doen't need delay, as the callback is fired in next event loop,just next when current event loop finishes.
It is associated with global module "process" in Node.js.
It can be cancelled by return function of process.nextTick().

var placeholder = process.nextTick(functionThatNeverGetsCalled);
placeholder.cancel()
OR
var cancel = process.nextTick(functionThatNeverGetsCalled);
cancel();


setTimeout():
setTimeout() function is specific to the JavaScript runtime Event queue.
It takes two arguments one is callback and another one is a delay in milisecond.
It is associated with global module "timers" in Node.js.
It can be cancelled anytime using clearTimeout(timeout).


4. What is timer in Node.js ?


Its a global module in Node.js

It provides API to handle delay and sleep in Node.js

It provides three methods to start a timer:
setImmediate(callback[, ...args])
setInterval(callback, delay[, ...args])
setTimeout(callback, delay[, ...args])

It provides three methods to cancel a timer:
clearImmediate(immediate)
clearInterval(timeout)
clearTimeout(timeout)


5. What is setTimeout() in Node.js ?


It Schedules the execution of a one-time callback after the specified delay in milliseconds.

Example:
const util = require('util');
const setTimeoutPromise = util.promisify(setTimeout);
setTimeoutPromise(100, 'xyz').then((value) => {
  // value === 'xyz' (passing the values is optional)
  // This will be executed after about 100 milliseconds.
});



6. What is setImmediate() in Node.js ?


It Schedules the "immediate" execution of the callback after I/O events' callbacks.

Example:
const util = require('util');
const setImmediatePromise = util.promisify(setImmediate);
setImmediatePromise('foobar').then((value) => {
  // value === 'foobar' (passing values is optional)
  // This is executed after all the I/O callbacks.
});
// or can be called with async function
async function timerExample() {
  console.log('Before I/O callbacks');
  await setImmediatePromise();
  console.log('After I/O callbacks');
}
timerExample();

util.promisify creates the promise to handle the asynchronous operation.


7. What is setInterval() in Node.js ?


It Schedules the repeated execution of callbacks after every specified delay in milliseconds.

Example:
const util = require('util');
const setIntervalPromise = util.promisify(setInterval);
setIntervalPromise(100, 'xyz').then((value) => {
  // value === 'xyz' (passing the values is optional)
  // This will be executed after every 100 milliseconds.
});


Comments:
Content Contributor
Prashant Surya

Prashant Surya

Software developer at Uniserve Data Technologies Pvt. Ltd.  |   Noida, Uttar Pradesh, India

I am a software developer, freelancer and trainer working on technologies likes. MEAN stack, MERN Stack, ASP.NET full stack , SQL, JavaScript, jQuery, HTML, CSS, SEO, responsive UI/UX designing... having 7 years of experience in development in multiple domains like, eCommerce, capital markets, education, real estate...


Read related articles

Node.js Interview Questions and Answers | Part 5

Pooja Goel  |   Dec 30, 2019  |  Views: 207

Node.js Interview Questions and Answers | Part 4

Pooja Goel  |   Dec 30, 2019  |  Views: 260

Node.js Interview Questions and Answers | Part 3

Pooja Goel  |   Dec 30, 2019  |  Views: 151

Node.js Interview Questions and Answers | Part 2

Pooja Goel  |   Dec 27, 2019  |  Views: 107

Node.js Interview Questions and Answers | Part 1

Pooja Goel  |   Dec 27, 2019  |  Views: 300

Want to sell your software ?-

If you want to sell your readymade software to the genuine clients or businessman, list your software with details and demo links.

  • Sell ERP
  • Sell Website Clones
  • Sell App Clones
  • Sell Readymade Portals
  • Sell Readymade Websites
  • Sell Mobile Apps
  • Sell Window Apps

Clients will find it using our advanced search filter and will contact you directly.

No any charge for the product lsiting.

Do you own a Company ?+

Property.sale

Buy or Sell Commercial Property+

Are you a freelancer ?+

Looking for a job ?+

Are you a Service Provider ?+

Are you a blogger ?+

Do you own an IT Institute ?+


Property.sale