Created
August 15, 2012 07:08
-
-
Save nomospace/3357290 to your computer and use it in GitHub Desktop.
node-imap 依次执行函数队列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var box, cmds, next = 0, | |
cb = function(err) { | |
if (err) die(err); | |
else if (next < cmds.length) cmds[next++].apply(this, Array.prototype.slice.call(arguments).slice(1)); | |
}; | |
cmds = [ | |
function() { | |
imap.connect(cb); | |
}, function() { | |
imap.openBox('INBOX', false, cb); | |
}, function(result) { | |
box = result; | |
imap.search(['SEEN', ['SINCE', 'August 15, 2012']], cb); | |
}, function(results) { | |
var fetch = imap.fetch(results, { | |
request: { | |
headers: ['from', 'to', 'subject', 'date'] | |
} | |
}); | |
fetch.on('message', function(msg) { | |
console.log('Got message: ' + util.inspect(msg, true, 5)); | |
msg.on('data', function(chunk) { | |
console.log('Got message chunk of size ' + chunk.length); | |
}); | |
msg.on('end', function() { | |
console.log('Finished message: ' + util.inspect(msg, false, 5)); | |
}); | |
}); | |
fetch.on('end', function() { | |
console.log('Done fetching all messages!'); | |
imap.logout(cb); | |
}); | |
} | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment