lib/imapSimple.js
function connect(options, callback) {
    options = options || {};
    options.imap = options.imap || {};

    // support old connectTimeout config option. Remove in v2.0.0
    if (options.hasOwnProperty('connectTimeout')) {
        console.warn('[imap-simple] connect: options.connectTimeout is deprecated. ' +
            'Please use options.imap.authTimeout instead.');
        options.imap.authTimeout = options.connectTimeout;
    }

    // set default authTimeout
    options.imap.authTimeout = options.imap.hasOwnProperty('authTimeout') ? options.imap.authTimeout : 2000;

    if (callback) {
        return nodeify(connect(options), callback);
    }

    return new Promise(function (resolve, reject) {
        var imap = new Imap(options.imap);

        function imapOnReady() {
            imap.removeListener('error', imapOnError);
            imap.removeListener('close', imapOnClose);
            imap.removeListener('end', imapOnEnd);
            resolve(new ImapSimple(imap));
        }

        function imapOnError(err) {
            if (err.source === 'timeout-auth') {
                err = new errors.ConnectionTimeoutError(options.imap.authTimeout);
            }

            imap.removeListener('ready', imapOnReady);
            imap.removeListener('close', imapOnClose);
            imap.removeListener('end', imapOnEnd);
            reject(err);
        }

        function imapOnEnd() {
            imap.removeListener('ready', imapOnReady);
            imap.removeListener('error', imapOnError);
            imap.removeListener('close', imapOnClose);
            reject(new Error('Connection ended unexpectedly'));
        }

        function imapOnClose() {
            imap.removeListener('ready', imapOnReady);
            imap.removeListener('error', imapOnError);
            imap.removeListener('end', imapOnEnd);
            reject(new Error('Connection closed unexpectedly'));
        }

        imap.once('ready', imapOnReady);
        imap.once('error', imapOnError);
        imap.once('close', imapOnClose);
        imap.once('end', imapOnEnd);

        if (options.hasOwnProperty('onmail')) {
            imap.on('mail', options.onmail);
        }

        if (options.hasOwnProperty('onexpunge')) {
            imap.on('expunge', options.onexpunge);
        }

        if (options.hasOwnProperty('onupdate')) {
            imap.on('update', options.onupdate);
        }

        imap.connect();
    });
}
function connect(options, callback) {
    options = options || {};
    options.imap = options.imap || {};

    // support old connectTimeout config option. Remove in v2.0.0
    if (options.hasOwnProperty('connectTimeout')) {
        console.warn('[imap-simple] connect: options.connectTimeout is deprecated. ' +
            'Please use options.imap.authTimeout instead.');
        options.imap.authTimeout = options.connectTimeout;
    }

    // set default authTimeout
    options.imap.authTimeout = options.imap.hasOwnProperty('authTimeout') ? options.imap.authTimeout : 2000;

    if (callback) {
        return nodeify(connect(options), callback);
    }

    return new Promise(function (resolve, reject) {
        var imap = new Imap(options.imap);

        function imapOnReady() {
            imap.removeListener('error', imapOnError);
            imap.removeListener('close', imapOnClose);
            imap.removeListener('end', imapOnEnd);
            resolve(new ImapSimple(imap));
        }

        function imapOnError(err) {
            if (err.source === 'timeout-auth') {
                err = new errors.ConnectionTimeoutError(options.imap.authTimeout);
            }

            imap.removeListener('ready', imapOnReady);
            imap.removeListener('close', imapOnClose);
            imap.removeListener('end', imapOnEnd);
            reject(err);
        }

        function imapOnEnd() {
            imap.removeListener('ready', imapOnReady);
            imap.removeListener('error', imapOnError);
            imap.removeListener('close', imapOnClose);
            reject(new Error('Connection ended unexpectedly'));
        }

        function imapOnClose() {
            imap.removeListener('ready', imapOnReady);
            imap.removeListener('error', imapOnError);
            imap.removeListener('end', imapOnEnd);
            reject(new Error('Connection closed unexpectedly'));
        }

        imap.once('ready', imapOnReady);
        imap.once('error', imapOnError);
        imap.once('close', imapOnClose);
        imap.once('end', imapOnEnd);

        if (options.hasOwnProperty('onmail')) {
            imap.on('mail', options.onmail);
        }

        if (options.hasOwnProperty('onexpunge')) {
            imap.on('expunge', options.onexpunge);
        }

        if (options.hasOwnProperty('onupdate')) {
            imap.on('update', options.onupdate);
        }

        imap.connect();
    });
}
function connect ( options , callback ) { options = options || { } ; options . imap = options . imap || { } ; // support old connectTimeout config option. Remove in v2.0.0 if ( options . hasOwnProperty ( 'connectTimeout' ) ) { console . warn ( '[imap-simple] connect: options.connectTimeout is deprecated. ' + 'Please use options.imap.authTimeout instead.' ) ; options . imap . authTimeout = options . connectTimeout ; } // set default authTimeout options . imap . authTimeout = options . imap . hasOwnProperty ( 'authTimeout' ) ? options . imap . authTimeout : 2000 ; if ( callback ) { return nodeify ( connect ( options ) , callback ) ; } return new Promise ( function ( resolve , reject ) { var imap = new Imap ( options . imap ) ; function imapOnReady ( ) { imap . removeListener ( 'error' , imapOnError ) ; imap . removeListener ( 'close' , imapOnClose ) ; imap . removeListener ( 'end' , imapOnEnd ) ; resolve ( new ImapSimple ( imap ) ) ; } function imapOnError ( err ) { if ( err . source === 'timeout-auth' ) { err = new errors . ConnectionTimeoutError ( options . imap . authTimeout ) ; } imap . removeListener ( 'ready' , imapOnReady ) ; imap . removeListener ( 'close' , imapOnClose ) ; imap . removeListener ( 'end' , imapOnEnd ) ; reject ( err ) ; } function imapOnEnd ( ) { imap . removeListener ( 'ready' , imapOnReady ) ; imap . removeListener ( 'error' , imapOnError ) ; imap . removeListener ( 'close' , imapOnClose ) ; reject ( new Error ( 'Connection ended unexpectedly' ) ) ; } function imapOnClose ( ) { imap . removeListener ( 'ready' , imapOnReady ) ; imap . removeListener ( 'error' , imapOnError ) ; imap . removeListener ( 'end' , imapOnEnd ) ; reject ( new Error ( 'Connection closed unexpectedly' ) ) ; } imap . once ( 'ready' , imapOnReady ) ; imap . once ( 'error' , imapOnError ) ; imap . once ( 'close' , imapOnClose ) ; imap . once ( 'end' , imapOnEnd ) ; if ( options . hasOwnProperty ( 'onmail' ) ) { imap . on ( 'mail' , options . onmail ) ; } if ( options . hasOwnProperty ( 'onexpunge' ) ) { imap . on ( 'expunge' , options . onexpunge ) ; } if ( options . hasOwnProperty ( 'onupdate' ) ) { imap . on ( 'update' , options . onupdate ) ; } imap . connect ( ) ; } ) ; }
Connect to an Imap server, returning an ImapSimple instance, which is a wrapper over node-imap to
simplify it's api for common use cases.

@param {object} options
@param {object} options.imap Options to pass to node-imap constructor 1:1
@param {function} [callback] Optional callback, receiving signature (err, connection)
@returns {undefined|Promise} Returns a promise when no callback is specified, resolving to `connection`
Connect to an Imap server returning an ImapSimple instance which is a wrapper over node - imap to simplify it s api for common use cases .