Created
September 1, 2010 13:54
-
-
Save proppy/560712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
var s = ''; | |
var append_char = function(arg) { | |
s+= arg; | |
}; | |
function rendezvous(callback) { | |
return { | |
callbacks: [], | |
callback: function(cb) { | |
var rdv = this; | |
rdv.callbacks.push(cb); | |
return function() { | |
rdv.callbacks.splice(rdv.callbacks.indexOf(cb), 1)[0].apply(this, arguments); | |
if (rdv.callbacks.length === 0) { | |
callback(); | |
} | |
}; | |
} | |
}; | |
} | |
var rdv_up = rendezvous(function() { | |
assert.equal('3210', s); | |
console.log('OK'); | |
}); | |
var rdv = rendezvous(rdv_up.callback(function() { | |
append_char('0'); | |
})); | |
var f1 = rdv.callback(append_char); | |
var f2 = rdv.callback(append_char); | |
var f3 = rdv.callback(append_char); | |
f3('3'); | |
f2('2'); | |
f1('1'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment