Question: As the NodejS developer in the company, you have been given the task to write an Asynchronous Search Script that takes a search term as

As the NodejS developer in the company, you have been given the task to write an Asynchronous Search Script that takes a search term as input and returns the count of the matching objects or throws an error if the search was unsuccessful.Class DescriptionCreate a module, search.js, and implement the following set of functionalities:* The file should export a class Search as the default export of the module, and it should inherit from the EventEmitter class.* The class should have the following methods:* searchCount: Accepts the searchTerm[STRING] as the argument, and it internally makes use of the predefined APl service in order to fetch the count of the matches.* If the searchTerm passed to the function is undefined, the function should emit the event SEARCH_ERROR with an object containing the message propertyINVALID_TERM, and then immediately return without executing further.{ "message": "INVALID_TERM"}* The class should emit the following events:* SEARCH_STARTED - Emitted immediately, when the searchCount function is called and if searchTerm passed to the function is not undefined. searchTerm should be passed to the callback function of the event handler.* SEARCH_SUCCESS - Emitted if the countMatches method resolved with a result successfully. The callback function of the event handler should accept an object with the following properties:count: The count of the matches returned by theAPI. countMatches function term: The search term that was passed to the function* SEARCH_ERROR - Emitted if searchTerm passed to the searchCount function is undefined (as described above) or if the countMatches method rejects with an error.* When the countMatches method rejects with an error, the callback function of the event handler should accept an object with the following properties:message: The message property of the error object returned by theAPI. countMatches method term: The search term passed to the functionThe API service file, mock-api.js, is present in the root folder of the project and has a single method, countMatches, that accepts search Term as input and returns a Promise. The Promise either resolves with a counted number of the matches, or it rejects with a JavaScript Error object. To test rejection for this method, you can pass searchTerm'error' to the countMatches function.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!