Callback Functions in Action
Which of the following best describes a callback function in JavaScript?
- A class used for managing events
- A built-in JavaScript function for HTTP requests
- A function that is passed as an argument to another function and executed later
- A variable that stores asynchronous data
- A function that calls itself repeatedly
Understanding setTimeout
What does the setTimeout function do in JavaScript?
- Repeats a function continuously every set interval
- Runs a function only when the page loads
- Executes a function once after a specified delay
- Pauses JavaScript execution completely
- Waits until a promise is resolved and then returns
Promise Basics
Which status is NOT a valid state of a JavaScript Promise?
- resolved
- settled
- fulfilled
- rejected
- pending
Chaining Promises
What does the .then() method do when called on a promise?
- Converts the promise into a synchronous function
- Blocks code execution until the promise completes
- Creates a new unresolved promise immediately
- Terminates the promise chain
- Attaches a callback to execute when the promise is fulfilled
Error Handling with Promises
Which method is used to handle errors in a promise chain?
- catch()
- reject()
- error()
- abort()
- fail()
async/await Syntax
What does the async keyword do when added before a function declaration?
- Specifies error handling for the function
- Allows the use of await inside the function
- Forces the function to run immediately
- Automatically pauses function execution
- Converts the function to a callback
Using await with Promises
What does the await keyword do when used inside an async function?
- Runs code in the background without waiting
- Converts a regular function into a promise
- Automatically retries the awaited promise
- Pauses execution until the promise resolves
- Rejects the promise if a condition is met
Event Loop Operation
When does the JavaScript event loop move tasks from the task queue to the call stack?
- Whenever a variable is declared
- When the call stack is empty
- After each line of code runs
- Only when setTimeout is called
- Just before the page unloads
Microtasks vs Macrotasks
Which of the following is processed first by the event loop: microtasks or macrotasks?
- Neither, they require manual execution
- They run in alphabetical order
- Microtasks
- Macrotasks
- Both at the same time
Promise.all Behavior
What does Promise.all return if at least one of the promises is rejected?
- An array of undefined values
- A new callback function
- A rejected promise
- A resolved promise with empty results
- A warning message
Avoiding Callback Hell
Which feature of JavaScript helps avoid 'callback hell' by improving readability?
- Globals
- Synchronous functions
- Loops
- setStack
- Promises
setInterval Purpose
What is the role of setInterval in JavaScript?
- Closes the event loop
- Executes a function repeatedly at specified intervals
- Runs code once immediately
- Handles HTTP requests automatically
- Waits for a promise to resolve
Event Loop Definition
Which statement best describes the JavaScript event loop?
- It automatically handles errors in code
- It acts as a scheduler for synchronous code only
- It manages execution of multiple tasks by monitoring the call stack and task queues
- It ensures variables are always declared
- It provides native multithreading support
Promise.resolve Usage
What is the outcome of calling Promise.resolve(5)?
- An immediately rejected promise
- A rejected promise with value 5
- A delayed execution of the number 5
- A promise that resolves with the value 5
- A synchronous exception
asynchronous vs synchronous
In JavaScript, what is one key difference between asynchronous and synchronous code?
- Asynchronous code cannot handle errors
- Synchronous code is used only in event handlers
- Synchronous code uses callbacks, asynchronous does not
- Asynchronous code does not block further execution, while synchronous code does
- Asynchronous code only runs after page load