Skip to content

Instantly share code, notes, and snippets.

@pvdz
Created November 30, 2011 17:28
Show Gist options
  • Save pvdz/1409912 to your computer and use it in GitHub Desktop.
Save pvdz/1409912 to your computer and use it in GitHub Desktop.
// the case:
// runner.js
module.exports = {
arr: [],
fetch: function(str){ this.arr.push(str); }
};
// mod1.js
module.exports = function(){
setTimeout(function(){
var run = require('runner.js');
run.fetch('foo');
, Math.random() * 10000);
};
// mod2.js
module.exports = function(){
setTimeout(function(){
var run = require('runner.js');
run.fetch('bar');
, Math.random() * 10000);
};
// start.js
require('mod1.js')();
require('mod2.js')();
require('runner.js').arr; // either ['foo','bar'] or ['bar','foo'], doesn't matter for the case in hand
/*
This is a generalization, obviously (!)
Now imagine me requiring start.js twice in the same node instance. It doesn't matter why.
How do I get the same result? Specifically under node.js. (So runner.arr being either ['foo','bar'] or ['bar','foo'] but never have 'foo' or 'bar' twice.)
Note that mod1 and mod2 run async, so resetting the runner is not an option.
I'm not interested in meta ("You shouldn't do this"), please tell me how you _would_ accomplish this.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment