Mixed Interview questions Flashcards
(13 cards)
What is the execution order of the following in Node.js?
process.nextTick(() => console.log(‘tick’));
setImmediate(() => console.log(‘immediate’));
setTimeout(() => console.log(‘timeout’), 0);
Promise.resolve().then(() => console.log(‘promise’));
Execution order:
process.nextTick() – runs immediately after current operation, before microtasks.
Promise.resolve().then() – runs after nextTick, before timers.
setTimeout() – runs in the timers phase.
setImmediate() – runs in the check phase (may run before or after setTimeout based on environment).
process.nextTick(() => console.log(‘tick’));
Promise.resolve().then(() => console.log(‘promise’));
setTimeout(() => console.log(‘timeout’), 0);
setImmediate(() => console.log(‘immediate’));
Why can process.nextTick() starve the event loop?
Because nextTick callbacks run before every I/O phase. If you recursively schedule more process.nextTick(), it prevents the event loop from progressing, blocking timers, I/O, and microtasks.
Given the following code, what is the likely output?
setTimeout(() => console.log(‘timeout’), 0);
setImmediate(() => console.log(‘immediate’));
It depends on the environment:
In a script: timeout may run first.
Inside an I/O callback: immediate usually runs first.
Because they run in different phases (setTimeout in timers, setImmediate in check), the order isn’t deterministic outside of I/O.
What happens when you nest microtasks and nextTicks inside a setTimeout?
setTimeout(() => {
Promise.resolve().then(() => console.log(‘promise inside’));
process.nextTick(() => console.log(‘nextTick inside’));
console.log(‘timeout’);
}, 0);
Output:
timeout
nextTick inside – after current callback, before microtasks.
promise inside – after nextTick, before leaving the timer phase.
timeout
nextTick inside
promise inside
How does await relate to the event loop?
await is syntactic sugar for Promise.then(). It schedules the continuation as a microtask, which runs after the current task and process.nextTick, but before macrotasks like setTimeout.
What is the any type in TypeScript and when should it be avoided?
any disables type checking for the variable. It can be assigned any value and used in any way without compiler errors. It should be avoided in production code because it removes the safety guarantees of TypeScript.
What is the unknown type in TypeScript and how is it different from any?
unknown also accepts any value, but unlike any, it requires type checking before use. It’s a safer alternative to any when the type is not known at declaration time.
What is the never type and where is it used?
never represents values that should never occur. It is used for:
Functions that never return (e.g., throw errors)
Exhaustiveness checks in switch statements
Useful for catching unhandled cases at compile time.
Which is safer: any or unknown? Why?
unknown is safer because the compiler forces you to check the type before using it. any skips all checks and allows unsafe operations.
How would you use never to enforce exhaustive switch-case logic?
type Shape = ‘circle’ | ‘square’;
function handle(shape: Shape) {
switch (shape) {
case ‘circle’:
// handle circle
break;
case ‘square’:
// handle square
break;
default:
const _exhaustiveCheck: never = shape; // error if new type is added
}
}
Call() function
- call()
Invokes the function immediately.
You pass this as the first argument, followed by the actual arguments.
Example:
function greet(greeting, name) {
console.log(${greeting}, ${name}. From:
, this.company);
}
const context = { company: ‘OpenAI’ };
greet.call(context, ‘Hello’, ‘Alice’);
// Output: “Hello, Alice. From: OpenAI”
Apply function
apply()
Just like call, but it takes arguments as a single array.
Example:
greet.apply(context, [‘Hi’, ‘Bob’]);
// Output: “Hi, Bob. From: OpenAI”
Bind function
bind()
Does not invoke the function immediately.
Instead, it returns a new function with this permanently bound.
Example:
const boundGreet = greet.bind(context, ‘Hey’);
boundGreet(‘Charlie’);
// Output: “Hey, Charlie. From: OpenAI”
You can reuse boundGreet as many times as you want.