Node.js: Asynchronous Programming

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/38

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

39 Terms

1
New cards

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

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

2
New cards

How does setTimeout() differ from setInterval()?

setTimeout() runs a function once after a delay

3
New cards

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

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

4
New cards

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

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

5
New cards

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

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

6
New cards

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

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

7
New cards

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

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

8
New cards

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

Overlapping execution if a function takes longer than the interval.

9
New cards

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

By queuing functions

10
New cards

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

Both execute a function immediately after the current execution completes.

11
New cards

Why is setImmediate() not standard in browsers?

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

12
New cards

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

When you need to repeat a task at regular intervals.

13
New cards

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

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

14
New cards

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

By deferring function execution to avoid blocking the event loop.

15
New cards

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

Nothing happens; it silently fails.

16
New cards

How can you debug overlapping execution in setInterval()?

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

17
New cards

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

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

18
New cards

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

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

19
New cards

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

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

20
New cards

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

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

21
New cards

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

22
New cards

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

23
New cards

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

24
New cards

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

25
New cards

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

26
New cards

What is setTimeout used for?

Schedules a callback to execute after a specified delay.

27
New cards

What is setInterval used for?

Schedules a callback to execute repeatedly at specified intervals.

28
New cards

What does process.nextTick do?

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

29
New cards

What does setImmediate do?

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

30
New cards

How do you read a file asynchronously?

"Use fs.readFile(filePath

31
New cards

What is an EventEmitter?

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

32
New cards

How do you promisify a callback-based function?

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

33
New cards

What is the difference between process.nextTick and setImmediate?

"process.nextTick executes before I/O events

34
New cards

How do you handle errors in callbacks?

"Follow the error-first convention: callback(error

35
New cards

What is the purpose of a middleware function?

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

36
New cards

How do you ensure clean application shutdown?

"Handle signals (e.g.

37
New cards

What is an I/O poller?

"A function that manages long-running asynchronous operations

38
New cards

How do you register an event on an EventEmitter?

"Use emitter.on(event

39
New cards

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

Scheduling delayed operations followed by immediate I/O callbacks.