Occasionally, you have a race condition between two asynchronous functions where one must run after the other. This atomic event emitter is built just for that.
var atomic = {s:{},after:function(n,f,c){c=this.s[n]=this.s[n]||[];c==1?f():c.push(f)},fire:function(n,c,f){for(c=this.s[n]||[];f=c.pop();)f();this.s[n]=1}};
setTimeout(function () {
atomic.fire('first');
}, 100);
setTimeout(function () {
atomic.after('first', function () {
console.log('First must have run before this');
});
}, 70 + (Math.random() * 60));
This code is not tested anywhere (browsers and node).
You know, this doesn't really solve your problem. For example it's totally possible that
push()
is called after the channel was set totrue
. Or two calls offire
can call the function twice before the channel is set totrue
. However, nice snipped and nice style.