lib/inbox.js
function(imapMessage) {
    var deferred = Q.defer();
    var message = new Message();

    imapMessage.on('body', function(stream, info) {
        var buffer = '';

        stream.on('data', function(chunk) {
            buffer += chunk.toString('utf8');
        });

        stream.on('end', function() {
            if (info.which === 'TEXT') {
                message.body = buffer;
            } else {
                message.headers = Imap.parseHeader(buffer);
            }
        });
    });

    imapMessage.on('attributes', function(attrs) {
        message.attributes = attrs;
    });

    imapMessage.on('end', function() {
        deferred.resolve(message);
    });

    return deferred.promise;
}
function(imapMessage) {
    var deferred = Q.defer();
    var message = new Message();

    imapMessage.on('body', function(stream, info) {
        var buffer = '';

        stream.on('data', function(chunk) {
            buffer += chunk.toString('utf8');
        });

        stream.on('end', function() {
            if (info.which === 'TEXT') {
                message.body = buffer;
            } else {
                message.headers = Imap.parseHeader(buffer);
            }
        });
    });

    imapMessage.on('attributes', function(attrs) {
        message.attributes = attrs;
    });

    imapMessage.on('end', function() {
        deferred.resolve(message);
    });

    return deferred.promise;
}
Helper function that asynchronously converts ImapMessage instance
to instance of Message class.

@param  {ImapMessage} imapMessage    Instance of ImapMessage class obtained from Imap server call
@return {Promise}                    Promise resolved with instance of Message class