Bird
0
0

Which of the following is the correct way to use setImmediate to schedule a callback function in Node.js?

easy📝 Syntax Q3 of 15
Node.js - Timers and Scheduling
Which of the following is the correct way to use setImmediate to schedule a callback function in Node.js?
AsetImmediate = () => console.log('Hello');
BsetImmediate(console.log('Hello'));
CsetImmediate.call(console.log('Hello'));
DsetImmediate(() => { console.log('Hello'); });
Step-by-Step Solution
Solution:
  1. Step 1: Understand setImmediate usage

    setImmediate expects a function as its first argument to schedule it for execution after the current poll phase.
  2. Step 2: Analyze options

    setImmediate(() => { console.log('Hello'); }); correctly passes an anonymous arrow function as the callback.
    setImmediate(console.log('Hello')); immediately invokes console.log and passes its return value (undefined) to setImmediate, which is incorrect.
    setImmediate.call(console.log('Hello')); attempts to call setImmediate.call incorrectly.
    setImmediate = () => console.log('Hello'); reassigns setImmediate to a function, which is not scheduling a callback.
  3. Final Answer:

    setImmediate(() => { console.log('Hello'); }); -> Option D
  4. Quick Check:

    Callback must be a function [OK]
Quick Trick: Pass a function, not the result, to setImmediate [OK]
Common Mistakes:
  • Passing the result of console.log instead of a function
  • Using setImmediate.call incorrectly
  • Overwriting setImmediate instead of calling it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes