What is the main purpose of setTimeout() in JavaScript?
To delay the execution of a function by a specified number of milliseconds.
How does setTimeout() differ from setInterval()?
setTimeout() runs a function once after a delay
What does it mean to pass a delay of 0 to setTimeout()?
The function executes as soon as possible after the current execution context.
How do you pass parameters to a function used with setTimeout()?
Provide the function name and parameters after the delay as additional arguments.
What is returned by setTimeout() and setInterval()?
Both return an ID that can be used to cancel their execution.
How can you cancel a scheduled execution of setTimeout() or setInterval()?
Use clearTimeout() or clearInterval() with the returned ID.
Why would you use recursive setTimeout() instead of setInterval()?
To ensure the next call happens only after the current execution finishes.
What issues might arise when using setInterval() with functions that have varying execution times?
Overlapping execution if a function takes longer than the interval.
How can setTimeout() help in managing CPU-intensive tasks?
By queuing functions
How is setImmediate() in Node.js similar to setTimeout() with a delay of 0?
Both execute a function immediately after the current execution completes.
Why is setImmediate() not standard in browsers?
It's specific to Node.js and not part of the JavaScript standard.
In what scenarios would you use setInterval() instead of setTimeout()?
When you need to repeat a task at regular intervals.
How can you auto-stop an interval from running using clearInterval()?
Call clearInterval() inside the setInterval() callback based on a condition.
How might setTimeout() be used to queue functions in the scheduler?
By deferring function execution to avoid blocking the event loop.
What would happen if a clearTimeout() or clearInterval() is called with an invalid ID?
Nothing happens; it silently fails.
How can you debug overlapping execution in setInterval()?
Log timestamps or use recursive setTimeout() to control timing.
When would it be better to avoid setInterval() and use recursive setTimeout()?
When function execution time is variable or depends on external conditions.
How can setTimeout() and setInterval() improve the responsiveness of your JavaScript applications?
By deferring or pacing tasks to prevent blocking the event loop.
Are there any browser compatibility issues to consider with setTimeout() and setInterval()?
They are widely supported but might behave differently in older browsers.
How do setTimeout() and setInterval() interact with the JavaScript Event Loop?
They queue callbacks to be executed when the event loop is idle.
What is the primary reason Node.js is designed to be non-blocking?
To maximize server efficiency and handle a large number of concurrent connections with minimal resource overhead
In server-side applications, blocking I/O operations can severely limit scalability. Traditional multi-threaded servers create a new thread for each connection, which consumes significant memory and computational resources. Node.js's non-blocking, event-driven architecture allows a single thread to handle thousands of concurrent connections efficiently.
Key benefits include:
Extremely high concurrency with a low memory footprint
Efficient handling of I/O-intensive operations like network requests, database queries
Minimal context switching overhead
Ability to serve more clients with fewer system resources
What is the primary reason JavaScript is designed to be non-blocking on the main thread?
To prevent UI freezing and ensure responsive user interfaces, especially in web browsers.
In web applications, the main thread handles both UI rendering and JavaScript execution. If a long-running operation were to block this thread, the entire browser window would become unresponsive
What is Asynchronous Programming?
is a programming paradigm that allows tasks to run independently of the main program flow, enabling the program to continue executing other tasks without waiting for the completion of those tasks.
⚠ enables concurrency and parallelism
How does using callbacks help manage asynchronous operations in JavaScript?
Callbacks are functions passed as arguments to other functions, which are executed once an asynchronous operation completes. They solve the challenge of handling time-consuming operations without blocking the main thread.
Key mechanisms of callbacks:
Allow code to continue executing while waiting for an operation to finish
Provide a way to specify what should happen after an asynchronous task completes
Enable sequential execution of asynchronous tasks through nesting or chaining
What are some common challenges associated with using callbacks in complex procedures?
"callback hell" - deeply nested asynchronous calls
Error handling becomes complex
Less readable and harder to manage for complex async workflows
What is setTimeout used for?
Schedules a callback to execute after a specified delay.
What is setInterval used for?
Schedules a callback to execute repeatedly at specified intervals.
What does process.nextTick do?
Schedules a callback to execute after the current phase of the event loop.
What does setImmediate do?
Schedules a callback to execute after I/O events but before timers.
How do you read a file asynchronously?
"Use fs.readFile(filePath
What is an EventEmitter?
A class in Node.js used for implementing event-driven architecture.
How do you promisify a callback-based function?
Use util.promisify(apiFunction) to convert it into a promise-based function.
What is the difference between process.nextTick and setImmediate?
"process.nextTick executes before I/O events
How do you handle errors in callbacks?
"Follow the error-first convention: callback(error
What is the purpose of a middleware function?
Middleware functions process requests in a chain before reaching the final handler.
How do you ensure clean application shutdown?
"Handle signals (e.g.
What is an I/O poller?
"A function that manages long-running asynchronous operations
How do you register an event on an EventEmitter?
"Use emitter.on(event
What is a practical use of setTimeout with process.nextTick?
Scheduling delayed operations followed by immediate I/O callbacks.