mcavage/node-ldapjs — Issue #58: problem with "createClient" then "bind" if the connection is slow or unavailable
Author: trentm
Created: 2012-02-17 20:21:08+00:00
State: open

I have this code:

```
var server = ldap.createClient({
url: config.ufds.url
});

try {
server.bind(config.ldap.rootDn, config.ldap.password, function(err) {
if (err) {
console.log("XXX error binding to LDAP", err)
return callback(err);
}
console.log("XXX here")
var app;
try {
app = new App(config, server, log);
} catch(err) {
return callback(err);
}
return callback(null, app);
});
} catch(e) {
console.log("XXX boom:", e)
}
```

... and I *don't* have a LDAP server running at the configured url. The undesired result is that my callback to `bind` returns before there is a connection. Either my app hangs there or exits quickly.

Digging through ldapjs' client.js a bit (ldapjs version 0.4.8, the latest), it looks like:

- creating the client results in attempt to connect right away: https://github.com/mcavage/node-ldapjs/blob/master/lib/client.js#L147 This sits waiting for the timeout.

- The I do a bind attempt... which attempts a Client.connect ... which returns right away because `this.connection` is set ... which then attempts a BindRequest (https://github.com/mcavage/node-ldapjs/blob/master/lib/client.js#L238)

... after which I'm not really sure what is happening.

Naively I'd say the automatic attempt to connect in the `Client` constructor should be removed.