I want to get a URI, parse it, and then get another URI based on the parsed data. Using restler, I thought I could do the parsing and the second get call from the .on('complete',...) function of the first get call. It doesn't seem to work as I expect. To investigate, I coded up the following, which is a very simplified version of how my code is structured. It never exits until I hit Ctrl-C.
var sys = require('util');
var rest = require('restler');
function UriCall( uri, handler ) {
rest.get(uri).on('complete', function(result) {
if (result instanceof Error) {
sys.puts('Error: ' + result.message);
}
else {
if (handler) { handler( result ); }
else { sys.puts('No handler provided.'); }
}
});
}
var uri = "http://www.google.com";
UriCall(uri, function(result) {
sys.print("@");
UriCall(uri, function(result) {
sys.print(".");
});
});
Output:
$node resttest.js
@@.@..@...@....@.....@......@.......@........@ [deleted error messages] .........@..........@...........@............@.............@..............@...............@................@.................@..................@...................@....................@.....................@......................@.......................@........................@.........................@..........................@...........................@............................@.............................@..............................@...............................@................................@.................................@..................................@................................... ^C
From the output above it looks like something is getting stacked up with each call and it just gets deeper and deeper. I'm a C/C++ guy who is just getting started with js/node/restler. Why doesn't the program simply call UriCall twice and then exit?
(BTW: The errors messages start with "(node) warning: possible EventEmitter memory leak detected 11 listeners added. Use emitter.setMaxListeners() to increase limit." and then included some trace. The error message doesn't surprise me given what appears to be happening.)