diff --git a/lib/imap.js b/lib/imap.js index 0329ba9..1a34d39 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -129,7 +129,6 @@ ImapConnection.prototype.connect = function(loginCb) { state.conn.cleartext = state.conn; state.conn.on('connect', function() { - clearTimeout(state.tmrConn); state.connected = true; state.authenticated = false; self.debug&&self.debug('[connection] Connected to host.'); @@ -488,6 +487,7 @@ ImapConnection.prototype.connect = function(loginCb) { var code = m[2]; if (state.status === STATES.NOAUTH) { if (!state.isReady) { + clearTimeout(state.tmrConn); state.isReady = true; state.conn.emit('ready'); } @@ -666,8 +666,11 @@ ImapConnection.prototype.connect = function(loginCb) { state.conn.cleartext.on('data', ondata); state.conn.connect(this._options.port, this._options.host); - state.tmrConn = setTimeout(this._fnTmrConn.bind(this, loginCb), - this._options.connTimeout); + + state.tmrConn = setTimeout(function() { + state.conn.destroy(); + loginCb(new Error('Connection timed out')); + }, this._options.connTimeout); }; ImapConnection.prototype.isAuthenticated = function() { @@ -1274,11 +1277,6 @@ ImapConnection.prototype.__defineGetter__('seq', function() { // Private/Internal Functions -ImapConnection.prototype._fnTmrConn = function(loginCb) { - loginCb(new Error('Connection timed out')); - this._state.conn.destroy(); -}; - ImapConnection.prototype._serverSupports = function(capability) { return (this.capabilities.indexOf(capability) > -1); };