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

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)


Loading IDE...