var client = new node.http.Client(5984);
function fetch(data) {
return {
execute : function(groupHandler) {
var fetchCounter = 0;
for (var key in data) {
if (data.hasOwnProperty(key)) {
var job = data[key];
var path = job[0];
var callHandler = job[1];
fetchCounter += 1;
client.get(path).finish(function(response) {
puts("Finishing request to " + path);
response.setBodyEncoding("utf8");
var result = "";
response.onBody = function(chunk) {
result += chunk;
};
response.onBodyComplete = function() {
data[key] = callHandler(response, result);
fetchCounter -= 1;
if (fetchCounter === 0) {
puts("Group Handler");
groupHandler(data);
}
};
});
}
};
}
};
}
fetch({
foo : [
"/",
function(response,result) {
return result;
}
]
}).execute(function(result) {
fetch({
foo : [
"/",
function(response,result) {
return result;
}
]
}).execute(function(result) {
puts("Success");
});
});
---
brickmirror:proxy sixtus$ node /Users/sixtus/temp/clientbug.js