Node.js: Asynchronous Programming

studied byStudied by 1 person
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 38

flashcard set

Earn XP

Description and Tags

39 Terms

1

What is the main purpose of setTimeout() in JavaScript?

To delay the execution of a function by a specified number of milliseconds.

New cards
2

How does setTimeout() differ from setInterval()?

setTimeout() runs a function once after a delay

New cards
3

What does it mean to pass a delay of 0 to setTimeout()?

The function executes as soon as possible after the current execution context.

New cards
4

How do you pass parameters to a function used with setTimeout()?

Provide the function name and parameters after the delay as additional arguments.

New cards
5

What is returned by setTimeout() and setInterval()?

Both return an ID that can be used to cancel their execution.

New cards
6

How can you cancel a scheduled execution of setTimeout() or setInterval()?

Use clearTimeout() or clearInterval() with the returned ID.

New cards
7

Why would you use recursive setTimeout() instead of setInterval()?

To ensure the next call happens only after the current execution finishes.

New cards
8

What issues might arise when using setInterval() with functions that have varying execution times?

Overlapping execution if a function takes longer than the interval.

New cards
9

How can setTimeout() help in managing CPU-intensive tasks?

By queuing functions

New cards
10

How is setImmediate() in Node.js similar to setTimeout() with a delay of 0?

Both execute a function immediately after the current execution completes.

New cards
11

Why is setImmediate() not standard in browsers?

It's specific to Node.js and not part of the JavaScript standard.

New cards
12

In what scenarios would you use setInterval() instead of setTimeout()?

When you need to repeat a task at regular intervals.

New cards
13

How can you auto-stop an interval from running using clearInterval()?

Call clearInterval() inside the setInterval() callback based on a condition.

New cards
14

How might setTimeout() be used to queue functions in the scheduler?

By deferring function execution to avoid blocking the event loop.

New cards
15

What would happen if a clearTimeout() or clearInterval() is called with an invalid ID?

Nothing happens; it silently fails.

New cards
16

How can you debug overlapping execution in setInterval()?

Log timestamps or use recursive setTimeout() to control timing.

New cards
17

When would it be better to avoid setInterval() and use recursive setTimeout()?

When function execution time is variable or depends on external conditions.

New cards
18

How can setTimeout() and setInterval() improve the responsiveness of your JavaScript applications?

By deferring or pacing tasks to prevent blocking the event loop.

New cards
19

Are there any browser compatibility issues to consider with setTimeout() and setInterval()?

They are widely supported but might behave differently in older browsers.

New cards
20

How do setTimeout() and setInterval() interact with the JavaScript Event Loop?

They queue callbacks to be executed when the event loop is idle.

New cards
21

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

New cards
22

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

New cards
23

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

New cards
24

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

New cards
25

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

New cards
26

What is setTimeout used for?

Schedules a callback to execute after a specified delay.

New cards
27

What is setInterval used for?

Schedules a callback to execute repeatedly at specified intervals.

New cards
28

What does process.nextTick do?

Schedules a callback to execute after the current phase of the event loop.

New cards
29

What does setImmediate do?

Schedules a callback to execute after I/O events but before timers.

New cards
30

How do you read a file asynchronously?

"Use fs.readFile(filePath

New cards
31

What is an EventEmitter?

A class in Node.js used for implementing event-driven architecture.

New cards
32

How do you promisify a callback-based function?

Use util.promisify(apiFunction) to convert it into a promise-based function.

New cards
33

What is the difference between process.nextTick and setImmediate?

"process.nextTick executes before I/O events

New cards
34

How do you handle errors in callbacks?

"Follow the error-first convention: callback(error

New cards
35

What is the purpose of a middleware function?

Middleware functions process requests in a chain before reaching the final handler.

New cards
36

How do you ensure clean application shutdown?

"Handle signals (e.g.

New cards
37

What is an I/O poller?

"A function that manages long-running asynchronous operations

New cards
38

How do you register an event on an EventEmitter?

"Use emitter.on(event

New cards
39

What is a practical use of setTimeout with process.nextTick?

Scheduling delayed operations followed by immediate I/O callbacks.

New cards
robot