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.
});