Exercise: A Tamper-Free Queue

Practice how to create a promise-based Queue class with the enqueue() and dequeue() methods.

Problem statement

Create a Queue class that has only one dequeue() named publicly accessible method. Such a method returns Promise that resolves with a new element extracted from an internal queue data structure. If the queue is empty, then Promise will resolve when a new item is added. The Queue class must also have a revealing constructor that provides a function called enqueue() to the executor that pushes a new element to the end of the internal queue. The enqueue() function can be invoked asynchronously and it must also take care of “unblocking” any eventual Promise returned by the dequeue() method. To try out the Queue class, you could build a small HTTP server into the executor function. Such a server would receive messages or tasks from a client and would push them into the queue. A loop would then consume all those messages using the dequeue() method.

Coding challenge

Write your solution code in the following code widget. We’ve already added the package.json file for your ease.

Get hands-on with 1400+ tech skills courses.