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).
Well that's the point right?
If the second timeout is < 100ms, the actual function is triggered by atomic after the first one runs.
If it is >= 100ms, it just runs immediately.
You are right though, there is no proof of sequence -- only proof that the second function runs. I will toss that in.