Skip to content

Commit

Permalink
Never call back twice. Fixes #159.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Apr 9, 2013
1 parent 5ff14f5 commit 13ed446
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,35 @@ var BUCKET_OPS_MAX = 1000;
* @param {Request} req The http request
* @param {Function} fn(err, res) The callback function.
* err - The exception if an exception occurred while sending the http
* request (for example if internet connection was lost).
* request (for example if internet connection was lost).
* res - The http response if no exception occurred.
* @api private
*/
function registerReqListeners(req, fn){
req.on('response', function(res){ fn(null, res); });
req.on('error', fn);
var hasCalledBack = false;

function cleanup() {
hasCalledBack = true;
req.removeListener('response', onResponse);
req.removeListener('error', onError);
}

function onResponse(res){
if (!hasCalledBack) {
fn(null, res);
}
cleanup();
}

function onError(err){
if (!hasCalledBack) {
fn(err);
}
cleanup();
}

req.on('response', onResponse);
req.on('error', onError);
}

function ensureLeadingSlash(filename) {
Expand Down

0 comments on commit 13ed446

Please sign in to comment.