Explore key concepts and features of asynchronous programming in JavaScript, covering callbacks, promises, async/await, and event handling for frontend development.
What does the setTimeout function do in JavaScript?
Explanation: setTimeout schedules a function to run once after a given delay. It does not repeat like setInterval, which is a common confusion. JavaScript does not support pausing execution (option 3) or thread interruption (option 4) with setTimeout.
Which statement accurately describes a callback function in JavaScript?
Explanation: A callback function is passed into another function to be called at a future point. It does not stop execution (option 2), is not limited to built-in or async code (option 3), and does not run immediately after declaration (option 4).
What are the three possible states of a JavaScript Promise?
Explanation: Promises can be in one of three states: pending (initial), fulfilled (resolved successfully), or rejected (failed). The other options use incorrect or unrelated terms for the standard Promise states.
Why are async and await keywords useful in JavaScript?
Explanation: Async and await make asynchronous code appear more like synchronous code, improving readability. They do not make code execute faster (option 2), do not block the main thread (option 3), and are not required for AJAX (option 4).
What is the primary role of the JavaScript event loop?
Explanation: The event loop coordinates the execution of asynchronous callbacks and tasks. It does not perform compilation (option 2), styling (option 3), or data storage (option 4).
Which statement best describes non-blocking code in JavaScript?
Explanation: Non-blocking code lets other processes continue while a task completes asynchronously. Blocking code (option 2) halts progress, synchronous code (option 3) may be blocking, and option 4 is unrelated.
What does Promise.all return if any of the promises it receives rejects?
Explanation: Promise.all rejects as soon as any included promise rejects, and does not wait for others. It does not return partial results (option 2), wait for all to resolve (option 3 if any rejected), or return undefined (option 4).
Which method is commonly used for making asynchronous HTTP requests in modern JavaScript?
Explanation: The fetch method is widely used for asynchronous HTTP requests. console.log writes to the console, localStorage.setItem stores data locally, and setInterval schedules repeated code execution.
Which method is used to handle errors in a JavaScript promise chain?
Explanation: The catch method is used for handling errors in promise chains. next is not a valid method for promises, resolve creates fulfilled promises, and final is not a standard method (the correct form is finally).