Friday, April 2, 2010

node.js in a Post-Promise World

‹prev | My Chain | next›

I ended last night's exploration of node.couch.js / node.js with this backtrace when attempting to query my CouchDB server for a list of DBs:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://localhost:5984
Error: finish() has been renamed to close() and no longer takes a response handler as an argument. Manually add a 'response' listener to the request object.
at ClientRequest.finish (http:317:9)
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:33:11
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:179:37
at Object.<anonymous> (/home/cstrom/repos/node.couch.js/changes/lib/service.js:190:3)
at Module._compile (node.js:721:23)
at node.js:749:20
at fs:51:23
at node.js:810:9
To resolve this, I change the allDbs function on line 33:
var alldbs = function (port, hostname, pathname) {
//var p = new events.Promise();
var p = {};
var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs', {'accept':'application/json'});
request.finish(function(response){
var buffer = '';
response.addListener("body", function(data){buffer += data});
response.addListener("complete", function(){
dbs = JSON.parse(buffer);
p['addCallback'] = function (callback) {
// call the callback
}
//p.emitSuccess(dbs);
})
})
return p
}
Rather than finishing the request, it now has to be closed and listeners added:
var alldbs = function (port, hostname, pathname) {
//var p = new events.Promise();
var p = {};
var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs', {'accept':'application/json'});
request.addListener('response', function(response){
var buffer = '';
response.addListener("body", function(data){buffer += data;});
response.addListener("complete", function(){
dbs = JSON.parse(buffer);
p['addCallback'] = function (callback) {
// call the callback
};
//p.emitSuccess(dbs);
});
});
request.close();

return p;
};
With that, I get:
cstrom@whitefall:~/repos/node.couch.js/changes$ ~/local/bin/node lib/service.js http://localhost:5984
TypeError: Object #<an Object> has no method 'addCallback'
at /home/cstrom/repos/node.couch.js/changes/lib/service.js:181:58
at Object.<anonymous> (/home/cstrom/repos/node.couch.js/changes/lib/service.js:192:3)
at Module._compile (node.js:721:23)
at node.js:749:20
at fs:51:23
at node.js:810:9
Progress!

Not quite where I'd like to be, but a step forward nonetheless. I am somewhat surprised that I am getting an error here, I do define the addCallback method in there:
// ...
p['addCallback'] = function (callback) {
// call the callback
};
// ...
I have changed two things in here, the addListener('response') and replacing the "promise" thingy from the other night. I spend the bulk of my night investigating the former. I eventually try the addListener in a standalone node.js script to see that it actually fires something off (which it does). But it will not work in the node.couch.js script...

Until I define the addCallback in the psuedo "promise" that I replaced the deprecated promise with:
var alldbs = function (port, hostname, pathname) {
//var p = new events.Promise();
var p = {
addCallback: function(foo) {
sys.puts('foo');
}
};

var client = http.createClient(port, hostname);
var request = client.request('GET', pathname + '_all_dbs');
request.addListener('response', function(response) {
//...
Tomorrow I need to figure out what "promises" in node.js used to be and how to replace them in this specific case. Ultimately I need whatever function is supplied to addCallback() to be called with the list of DBs on the CouchDB server. The code that is in there get the list of the DBs, but not until after the callback has already been invoked. This, I think is what the "promise" used to do. Now I just need to get it working in a post-promise world.

Day #62

2 comments:

  1. Hope this helps, a quote from the nodejs google group:

    If you need promises for legacy code, then include this file in your
    application: http://gist.github.com/310562.

    ReplyDelete
  2. @scott Oooh! That does help, thanks :)

    I think I'll give it a shot without promises first, but it is good to know that there is a fallback.

    ReplyDelete