How to implement Event Emitter in JavaScript? | Facebook Interview Question
@Yomesh Gupta

JavaScriptIntermediate
3974
Do you see value in our efforts?

Devtools Tech is a free platform for Frontend Engineers. It takes a lot of time, effort, and finanical resources to keep the platform running. If you see any value in our work and would like to support us then you can do using the following options.

InternationalDomestic

Create an event emitter that goes like this

var emitter = new Emitter();

Allows you to subscribe to some event

var sub1 = emitter.subscribe('function_name', callback1);
// you can have multiple callbacks to the same event
var sub2 = emitter.subscribe('function_name', callback2);

You can emit the event you want with this API (you can receive 'n' number of arguments)

emitter.emit('function_name', foo, bar);

And allows you to release the subscription like this (but you should be able to still emit from sub2)

sub1.release();

Loading IDE...