Skip to content

Instantly share code, notes, and snippets.

@tmpvar
Created March 31, 2010 00:58
Show Gist options
  • Save tmpvar/349804 to your computer and use it in GitHub Desktop.
Save tmpvar/349804 to your computer and use it in GitHub Desktop.
very basic
/*
+------------+
| A |
+-[ ]----[ ]-+
| |
| V
| +------------+
| | B |
| +-[ ]--------+
| |
V V <- - - - - - - - needs to synchronize here
+------------+
| C |
+------------+
Known:
- Structure needs to be precomputed
- Nodes emit promises (basically compiles a promise execution chain)
*/
// setup
var promise = require("node-promise"), sys = require("sys");
global.debug = function() { sys.puts(sys.inspect(arguments)); }
function A(out1, out2) {
return function() {
return function() {
debug("A was called");
var a = new promise.Promise();
var p1 = out1(a);
var p2 = out2; // a promise already
promise.when(a, function() {
debug("A fired when..");
p1.resolve("new A1");
p2.resolve("new A2");
});
setTimeout(a.resolve, 1000);
return a;
};
};
}
function B(out1) {
return function (in1) {
debug("B was called");
var _out1 = in1;
var b = new promise.Promise();
promise.when(in1, function() {
debug("B fired when");
b.resolve();
});
promise.when(b, function() {
_out1.resolve();
});
return b;
};
}
function C() {
return function(in1, in2) {
return function() {
debug("C was called");
var c = promise.all(in1,in2);
promise.when(c, function () {
debug("heh, C has completed");
});
return c;
};
};
}
// joins are special
var c = C();
var b = B(c);
var a = A(b,c);
debug(a(b,c(a,b))());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment