JS-Dev-101 practice question 20 of 25
A Node.js server library uses events and callbacks. The developer wants to log any issues the server has at boot time. Which code logs an error with an event?
Full set: 148 questionsIn Node.js, objects that emit events are instances of the EventEmitter class. The standard way to subscribe to an event is by using the .on() method. The first argument is the name of the event, and the second is a callback function to execute when the event is emitted. For handling errors in an event-driven architecture, the conventional event name is 'error'. This code correctly attaches a listener to the error event on the server object.
A. server.catch(...) is the syntax for handling promise rejections, not for listening to events from an EventEmitter.
B. server.error(...) is not a standard method on the EventEmitter class for listening to error events.
D. try...catch blocks are used for handling synchronous exceptions, not for handling asynchronously emitted 'error' events.
1. Node.js v18.17.1 Documentation, "Events" module, Class: EventEmitter, emitter.on(eventName, listener). Section: "The EventEmitter class".
2. Node.js v18.17.1 Documentation, "Error handling", Section: "Errors in synchronous vs. asynchronous code". It distinguishes between synchronous try...catch and asynchronous patterns like the 'error' event.
Keep practicing
Take the free JS-Dev-101 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other JS-Dev-101 practice questions
- Question 1A developer executes: document.cookie; document.cookie = 'key=John Smith'; What is the be…
- Question 2A developer is required to write a function that calculates the sum of elements in an arr…
- Question 3Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, r…
- Question 401 function Animal(size, type) { 02 this.type = type || 'Animal'; 03 this.canTalk = false…
- Question 5Refer to the code snippet: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i …
- Question 6Code: 01 let array = [1, 2, 3, 4, 4, 5, 4, 4]; 02 for (let i = 0; i < array.length; i++) …
