If you want to solve function no. 95/100 on your own, click on this link: https://www.100jsfunctions.com/exercises/createPubSub
If not, let's solve it together!
❓ How does it work?
This project is a collection of - you guessed it - 100 JavaScript exercises that will help you practice engineering fundamentals and improve your skills!
The platform has an interactive editor where you can code and then test your solutions when you're ready.
Practice all of them for free on: https://www.100JSFunctions.com/
📄 95. createPubSub
Write a function named `createPubSub` that returns an object implementing a basic Publish-Subscribe pattern.
The object - let's call it `pubSubBroker` - should have the following properties:
- `publish` - a method used to publish an event. It should accept 2 parameters:
- `eventId` - a string uniquely identifying the event
- `eventData` - any data that should be passed to the subscribers of the event
- `subscribe` - a method used to subscribe to a certain event. It should accept 2 parameters:
- `eventId` - a string uniquely identifying the event
- `callback(data)` - a function that will be called when the event is published. The function will receive one parameter - the eventData passed when publishing this specific event.
The function should return another function called unsubscribe that can be used to unsubscribe from this event.
#javascript #webdevelopment #frontend
1 Comments